oops in c++

30
OJECT ORINTED PROGRAMMING IN C++.

Upload: amritsinghmehra

Post on 15-Jul-2015

278 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: OOPS IN C++

OJECT

ORINTED

PROGRAMMING

IN

C++.

Page 2: OOPS IN C++

IN THE OOPS WE WILL PERFORMS VARIOUS COMPONENTS.

1)CLASS

2)OBJECT

3)ABSTRACT

4)ENCAPCULATION

5)INHERITANCE

Page 3: OOPS IN C++

CLASS

Class is a collection of member data and member function.

Member data is a collection of attribute in a class.

Member function is a collection of operation .It is perform in the class.

Page 4: OOPS IN C++

SYNTAX OF CLASSClass classname

{

Private:

All member data

Public:

All member function

};

Page 5: OOPS IN C++

EXAMPLE OF CLASS WITH THE HELP OF SUM OPERATION#include<conio.h>#include<iostream.h>Class sum{Private:

int a,b,c;Public:

void input(){Cout<<“enter the value of a and b variable”;Cin>>a>>b;}

Page 6: OOPS IN C++

Void display(){C=a+b;Cout<<“value of sun is”<<c;}};Void main(){Sum s;Clrscr();s.input();s.output();Getch();}

Page 7: OOPS IN C++

OBJECT OBJECTObject is a real entity world. With the help of OBJECT

we can call the member data and member function.

It is just like a membership.

Syntax of object:

class name classobject;

Classobject.mamber function();

Page 8: OOPS IN C++

EXAMPLE OF CLASS AND OBJECT WITH THE HELP OF SUM OPERATION

#include<conio.h>#include<iostream.h>Class sum{Private:

int a,b,c;Public:

void input(){Cout<<“enter the value of a and b variable”;Cin>>a>>b;}

Page 9: OOPS IN C++

Void display(){C=a+b;Cout<<“value of sun is”<<c;}};Void main(){Sum s; // Here (s) is a object . It is calling the member data and member

function.Clrscr();s.input();s.output();Getch();}

Page 10: OOPS IN C++

ABSTRACTAn abstract class is, conceptually, a class that cannot be

instantiated and is usually implemented as a class that has one or more pure virtual (abstract) functions.

pure virtual function is one which must be overriddenby any concrete (i.e., non-abstract) derived class. This is indicated in the declaration with the syntax " = 0" in the member function's declaration.

Page 11: OOPS IN C++

ENCAPCULATIONEncapsulation is the packing of data and functions into a

single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.

Best example of Encapsulations is class because it have the multiple of member data and member function in the same class.

Page 12: OOPS IN C++

INHERITANCEWith the help of inheritance we can reuse the data one

class to anther class .

Which have inherits the properties of base class is known as parents class/base class/super class.

Which have inherits the properties of parents class is known as sub class/child class/inherit class.

Inheritance seprate into many class…

1) Single Inheritance 4) Hybrid Inheritance

2) Hierarchical Inheritance 5) c

3) Multi Level Inheritance

Page 13: OOPS IN C++

Single Inheritancewhen a single derived class is created from a single base

class then the inheritance is called as single inheritance.

BASE CLASS

DERIVED CLASS

It is known as single inheritance class.

Page 14: OOPS IN C++

SYNTAX OF SINGLE INHERITANCEclass a

{Public:

all member data;Public:

all member function;};class b:public a{Public:

all member data;Public:

all member function;};

Page 15: OOPS IN C++

EXAMPLE OF SINGLE INHERITANCE#include<iostream.h>

#include<conio.h>

Class sum

{

Public:

Int a=5,b=5,c;

Public:

void display()

{

c=a+b;

Page 16: OOPS IN C++

cout<<“value of sum is”<<c;

}

};

class sub: public sum

{

Public:

Int d;

Public:

void output()

Page 17: OOPS IN C++

{d=a-b;

cout<<“the value of subtraction is”<<d;}

};void main()

{clrscr();sub s;s.display();s.output();getch();

}

Page 18: OOPS IN C++

Hierarchical Inheritancewhen more than one derived class are created from a

single base class, then that inheritance is called as hierarchical inheritance.

BASE CLASS

DERIVED CLASS (2)DERIVED CLASS (1)

Page 19: OOPS IN C++

SYNTAX OF HIEARCHICAL INHERITANCEClass a{Public: all member data;Public: all member function;};

Class b:public a{Public: all member data;Public: all member function;};

Page 20: OOPS IN C++

Class c:public a

{

Private: all member data;

Public: all member function;

};

Page 21: OOPS IN C++

EXAMPLE OF HIARCHICAL INHERITANCE

#include<conio.h>

#include<iostream.h>

Class a

{

public :

int a=10,b=2,c,d,e;

Public:

void output()

{

c=a-b;

Cout<<“value of sum is”<<c;

}

};

class b:public a

{

Public:

void display()

{

Page 22: OOPS IN C++

d=a-b;

Cout<<“value of subtraction is;

Cout<<d;

}

};

Class c:public

{

Public:

void coutput()

{

e=a*b;

Cout<<“multiplication is”<<e;

}

};

void main()

{

Clrscr();

a l;

l.output();

Page 23: OOPS IN C++

l.display();

l.coutput();

getch();

}

Page 24: OOPS IN C++

Multi Level Inheritancewhen a derived class is created from another derived

class, then that inheritance is called as multi level inheritance.

BASE CLASS

DERIVED CLASS (1)

DEEIVED CLASS(2)

Page 25: OOPS IN C++

SYNTAX OF MULTILEVEL INHERITANCEClass a

{

Public: all member data;

Public: all member function;

};

Class b:public a

{

Public:all member data;

Public:all member function;

};

Class c:public c

{

Private:all member data;

Private:all member function;

};

Page 26: OOPS IN C++

Hybrid InheritanceHybrid inheritance is combination of two or more

inheritances such as single,multiple,multilevel or Hierarchical inheritances.

DREVIED CLASS(1)

DERIVED CLASS(3)

DERIVED CLASS(2)

BASE CLASS

Page 27: OOPS IN C++

SYNTAX OF HYBRID INHERITANCEClass a

{

Public:all member data;

Public:all member function;

};

Class b:public a

{

Public:all member data;

Public:all member function;

};

Class c:public c

{

Public:all member data;

Public:all member function;

};

Class d:public d

{

Public:all member data;

Public:all member function;

};

Page 28: OOPS IN C++

Multiple InheritanceDeriving directly from more than one class is usually

called multiple inheritance.

BASE CLASS

DERVIED CLASS(1)

DERIVED CLASS(3)

DERIVED CLASS (2)

DERIVED CLASS(4)

Page 29: OOPS IN C++

SYNTAX OF MULTIPLE INHERITANCEClass a

{

Public:all memberdata;

Public:all member function;

};

Class b:public a

{

Public:all memberdata;

Public:all member function;

};

Class c:public a

{

Public:all memberdata;

Public:all member function;

};

Class d:public b

{

Public:all memberdata;

Public:all member function;

};

Page 30: OOPS IN C++

Class e:public c,public d

{

Private:all member data;

Public:all member function;

};www,cpd-india.com