Professional Documents
Culture Documents
Procedural: Object Oriented
Procedural: Object Oriented
Procedural: Object Oriented
Object Oriented
Procedural
C
Partial Fully
C++ JAVA
C+simula 67c++
#include<stdio.h>
#include<conio.h>
Void main()
{
Printf(hello);
Getch();
}
C++:-
#include<iostream.h>
#include<conio.h>
Void main()
{
Cout<<hello;
Getch();
}
OOPS:-
#include<iostream.h>
#include<conio.h>
Class A;
{
Void disp()
{
Cout<<hello;
}
};
Void main()
{
A obj;
Obj.disp();
Getch();
Return(0);
}
class classname
{
Access specifiers:
variable declration;
//data members.
access specifier:
function declraration;
//member functions
}
Object:-
Objects are instances of a given data type .the
data type provides a blueprint for the object
that is created ,or instantiated when the
application is executed.
Access specifier:
Used in class Used in outside
of class
Public Yes Yes
Private Yes No
Protected Yes No
Note: By default Access modifier is private.
For Eg.:
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.x=90;
obj.y=10;
obj.disp();
getch();
}
Eg:
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
Int a,b;
A obj;
Cout<<Enter two no.;
Cin>>obj.a;
Cin>>obj.b;
Obj.disp();
getch();
}
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input()
{
cout<<Enter two numbers;
cin>>x>>y;
}
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.input();
obj.disp();
getch();
}
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
void input()
{
cout<<Enter two numbers;
cin>>x>>y;
}
public:
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.input();
obj.disp();
getch();
}
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
void input()
{
cout<<Enter two numbers;
cin>>x>>y;
}
public:
void disp()
{
input();
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj;
obj.disp();
getch();
}
#include<iostream.h>
#include<conio.h>
class A
{
char n[20];
char mn[20],
char fn[20];
int m,r;
public:
void input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
#include<iostream.h>
#include<conio.h>
class A
{
char n[20];
char mn[20],
char fn[20];
int marks , roll;
public:
void input();
void disp();
};
void A::input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
cout<<Enter your marks;
cin>>marks;
cout<<Enter your Roll Numbers;
cin>>roll;
}
void A::disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}
void main()
{
A obj;
obj.input();
obj.disp();
getch();
}
void A::input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
cout<<Enter your marks;
cin>>m;
cout<<Enter your Roll Numbers;
cin>>r;
}
void A::disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}
void main()
{
A obj,k;
obj.input();
k.input();
obj.disp();
k.disp();
getch();
}
void A::input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your mothers name;
gets(mn);
cout<<Enter your marks;
cin>>m;
cout<<Enter your Roll Numbers;
cin>>r;
}
void A::disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<Mothers name is <<mn;
cout<<Roll Number is <<r;
cout<<Marks is <<m;
}
void main()
{
A obj[10];
Int I;
For(i=0;i<10;i++)
{
Obj[i].input();
}
for(i=0;i<10;i++)
{
obj[i].disp();
}
getch();
}
Example :-
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input(int a, int b )
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
A obj ;
obj.input(12,89);
obj.disp();
getch();
}
#include<stdio.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input(int a, int b )
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<sum is <<z;
}
};
void main()
{
int m,n;
cout<<Enter two number;
cin>>m>>n;
A obj ;
obj.input(m,n);
obj.disp();
getch();
}
Class A
{
Int x,y;
Public:
Void input(int a,int b)
{
X=a;
Y=b;
}
Int disp()
{
Return ( x + y ) ;
}
};
Void main()
{
Int r, m , n;
Cout<<enter the no. m and n;
Cin>>m>>n;
A obj;
Obj. input(); //ERROR
Obj. input (int m , int n);
R=obj.disp();
Cout<<sum=<<r;
}
1.
Class A
{
Int x,y,z;
Public:
Void input(int a,int b)
{
X=a;
Y=b;
}
Void disp()
{
Z=a+b;
Cout<<sum=<<z;
}
};
O/P: ERROR
2.
Class A
{
Int x,y,z;
Public:
Void input(int a,int b)
{
X=a;
Y=b;
}
Void disp()
{
Z=x+y;
Cout<<sum=<<z;
}
};
Void main()
{
A obj;
Obj.input(30.40);
Obj.disp();
}
O/P:
Sum=-12[Garbage value]
4.
Class A
{
Int x,y,z;
Public:
Void input(int x,int y)
{
This->x=x;
This->y=y;
}
Void disp()
{
Z=x+y;
Cout<<sum=<<z;
}
};
Void main()
{
A obj;
Obj.input(30,40);
Obj.disp();
}
O/P:
Sum=70
Constructor and Destructor
class A
{
int x=9; // error
int y=10; // error
public :
void disp()
{
cout<<x<<y;
}
};
class A
{
int x;
int y;
public :
void disp()
{
x=9;
y=90;
cout<<x<<y;
}
};
void main()
{
A obj
obj.disp();
getch();
}
Class A
{
Int x,y;
Public:
A()
{
X=10;
Y=20;
}
};
A obj; //constructor will be callled
class A
{
int x;
int y;
public :
A()
{
x=9;
y=90;
cout<<x<<y;
}
};
void main()
{
A obj
getch();
}
class A
{
int x;
int y;
private :
A()
{
x=9;
y=90;
cout<<x<<y;
}
};
void main()
{
A obj; //error
getch();
}
O/P
compilation error we cannot create object of
above class because constructor declared
under private section
Parameterized Constructor:-
class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
A obj; //error
A obj(90,80); //correct
getch();
}
Example 2:
class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
int m,n;
cout<<enter two number;
cin>>m>>n;
A obj(m,n);
Obj.disp();
getch();
}
class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
A obj; //error
A obj2(90,80); // correct
A k(4,7); // correct
A h(8,56) ; //correct
Obj2.disp();
k.disp();
h.disp();
getch();
}
class A
{
public:
A(int a=5)
{
}
};
void main()
{
A obj;
A kp(90);
getch();
}
}
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
A obj;
A obj2(90,80);
A k(4,7);
A h(8,56) ;
getch();
}
explicit call
A ob = A(20,80);
Constrcutor overloading:
A class can have more than one constructor
but signature are different. Signature means
type of parameter or number of parameter are
different.
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
Public:
A()
{
x=90;
y=100;
}
A(int a)
{
x=a;
y=2;
}
A(int a, int b)
{
x=a;
y=b;
}
void disp()
{
z=x+y;
cout<<Sum is<<z;
}
};
Void main()
{
A K1;
A K2(30,60);
A K3(1);
K1.disp();
K2.disp();
K3.disp();
getch();
}
Function overloading:
#include<stdio.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input( )
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;
}
void input(int a)
{
x=a;
y=2;
z=x+y;
cout<<Sum is<<z;
}
void input(float a)
{
x=a;
y=90;
z=x+y;
cout<<Sum is<<z;
}
};
Void main()
{
A K1;
K1.input();
K1.input(20.7);
K1.input(19);
getch();
}
Copy Constructor:- It is used for initial one
object member from another object member
with same value. Copy constructor present
in(classname &objectName)
class Sample
{
int x,y;
public:
Sample(int a, int b)
{
x=a;
y=b;
}
Sample(Sample &ob)
{
x=ob.x;
y=ob.y;
}
void show()
{
cout<<x<<\t<<y;
}
};
void main()
{
Sample s1(10,20);
Sample s2(s1);
s1.show();
s2.show();
getch();
}
class A
{
int x;
int y,z;
public :
A(int a, int b)
{
x=a;
y=b;
}
~A() //destructor
{
}
void disp()
{
z=x+y;
cout<<Sum is <<z;
}
};
void main()
{
int m,n;
cout<<enter two number;
cin>>m>>n;
A obj(m,n);
getch();
}
To define constructor outside boundary of
class
class A
{
int x,y;
public:
A();
void show();
};
A::A()
{
x=9;
y=8;
}
void A::show()
{
int z;
z=x+y;
cout<<Sum is <<z;
}
Function overloading:
A class can have more than one function of
same name but signature are different.
Signature means type of parameter or number
of parameter are different.
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input( )
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;
}
void input(int a)
{
x=a;
y=2;
z=x+y;
cout<<Sum is<<z;
}
void input(int a, int b)
{
x=a;
y=b;
z=x+y;
cout<<Sum is<<z;
}
void input(float a)
{
}
};
Void main()
{
A K1;
K1.input();
K1.input(20,40);
K1.input(1);
getch();
}
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input()
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;
}
void input()
{
x=9;
y=2;
z=x+y;
cout<<Sum is<<z;
}
void input()
{
x=a;
y=b;
z=x+y;
cout<<Sum is<<z;
}
};
Void main()
{
A K1;
K1.input();
K1.input();
K1.input();
getch();
}
Error
#include<iostream.h>
#include<conio.h>
class A
{
int x,y,z;
public:
void input()
{
x=90;
y=100;
z=x+y;
cout<<Sum is<<z;
}
int input()
{
x=9;
y=2;
z=x+y;
return z;
}
};
Void main()
{
int r;
A K1;
K1.input();
r=K1.input();
getch();
}
Static Member:-
o/p
0
0
0
0
1
2
1
2)
class A
{
static int x;
public:
A()
{
x=0;
}
void show()
{
cout<<x;
x++;
}
};
void main()
{
A a1,a2,a3,a4;
a1.show();
a2.show();
a3.show();
a4.show();
a1.show();
a1.show();
a2.show();
}
o/p
0
1
2
3
4
5
6
INHERITANCE:
Base class:
An existing class which property used by
derived or sub class .it is also known as parent
or super class.
Derived Class:
A newly defined class that use the property of
an existing class .
Inheritance present in many forms.
1. Single inheritance.
2. Multiple inheritance.
3. Hierarchal inheritance.
4. Multilevel inheritance.
5. Hybrid inheritance.
Single inheritance:
When a class inherits from only one base class
it is called single inheritance. 1 base class and
1 derived class
Multiple inheritance:
When a class inherits from more than one base
class it is called multiple inheritance.more
than 1 base class and 1 derived class.
Hierarchal inheritance:
When more than one class inherits from same
base it is called hierarchal inheritance.
Multilevel inheritance:
When a class inherits from a class who also
inherits from another class it is called
multilevel inheritance.
Hybrid inheritance:
It is a collection of two or more than
inheritance.
Single Inheritance:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void readdata()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void dispdata()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class employee: public person
{
Int id ;
Float sal;
Public:
Void input()
{
Cout<<enter id;
Cin>>id;
Cout<<enter salary;
Cin>>sal;
}
Void disp()
{
Cout<<id=<<id;
Cout<<\n salary=<<sal;
}
};
void main()
{
Employee obj;
Obj.readdata();
Obj.input();
Obj.dispdata();
Obj.disp();
Getch();
}
getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void readdata()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void dispdata()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class employee: public person
{
Int id ;
Float sal;
Public:
Void input()
{
Readdata()
Cout<<enter id;
Cin>>id;
Cout<<enter salary;
Cin>>sal;
}
Void disp()
{
Dispdata();
Cout<<id=<<id;
Cout<<\n salary=<<sa;l
}
};
void main()
{
Employee obj;
Obj.input;
Obj.disp();
Getch();
}
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void readdata()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void dispdata()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class employee: public person
{
Int id ;
Float sal;
Public:
Void input()
{
Cout<<enter id;
Cin>>id;
Cout<<enter salary;
Cin>>sal;
}
Void disp()
{
Cout<<id=<<id;
Cout<<\n salary=<<sa;l
}
};
void main()
{
Employee obj;
Obj.readdata();
Obj.input;
Obj.dispdata();
Obj.disp();
Getch();
}
Multiple Inheritance:
A class can inherits more than one class.
Syntax:
Derived class name: visibility mode base class
name, visibility mode base class name.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class student
{
Int r,m;
Int reg;
Public:
Void getdata()
{
Cout<<enter roll no.;
Cin>>r;
Cout<<enter marks;
Cin>>m;
Cout<<enter reg. no.;
Cin>>reg;
}
Void dispdata()
{
Cout<<\n your reg. no. is<<reg;
Cout<<\n your id is <<r;
Cout<<your marks is<<m;
}
};
Class graduate: public person, public student
{
Char st[20];
Public:
Void take()
{
Input();
Getdata();
Cout<<enter your stream;
Gets(st);
}
Void show()
{
Disp();
Dispdata();
Cou<<enter your stream=<<st;
}
};
void main()
{
graduate obj;
Obj.take();
Obj.show();
Getch();
}
Multilevel inheritance:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class person
{
char n[20];
char fn[20];
char city[30];
public:
void input()
{
cout<<Enter your name;
gets(n);
cout<<Enter your fathers name;
gets(fn);
cout<<Enter your city;
gets(city);
}
void disp()
{
cout<<\nDetails are\n;
cout<<Name is <<n;
cout<<\nFathers name is <<fn;
cout<<city is <<city;
}
};
Class student : public person
{
Int r,m;
Int reg;
Public:
Void getdata()
{
Cout<<enter roll no.;
Cin>>r;
Cout<<enter marks;
Cin>>m;
Cout<<enter reg. no.;
Cin>>reg;
}
Void dispdata()
{
Cout<<\n your reg. no. is<<reg;
Cout<<\n your id is <<r;
Cout<<your marks is<<m;
}
};
Class graduate: public student
{
Char st[20];
Void take()
{
Getdata();
Cout<<enter your stream;
Gets(st);
}
Void show()
{
Dispdata();
Cou<<enter your stream=<<st;
}
};
void main()
{
graduate obj;
Obj.take();
Obj.show();
Getch();
}
Public Derivation:
Private derivation:
Public yes no
Private no no
Protected yes no
Protected derivation:
Access Modifier used in derive Access by
object
of Derived
class
Public yes no
Private no no
Protected yes no
Class A
{
Int x,y;
Public:
Int a,b;
Protected:
Int m,n;
};
Class B; public A
{
Public:
Void disp()
X=5; //ERROR
A=10;
M=15;
}
};
Void main()
{
B obj;
Obj.b=13;
Obj.y=15; //ERROR
Obj.n=20; // ERROR
}
Constructor in inheritance:
1.) The base class constructor is constructed
before the derived class constructor
means when we create instance (object)
of derived class the base class
constructor will be called.
2.) If base class has default constructor than
it is not mandatory to derived class to
have constructor but when base class has
parameterized constructor than it is
mandatory to derived class to have
constructor because derived class
constructor pass value for base class
constructor.
Class A
{
Public:
A()
{
Cout<<const of A;
}
Void disp()
{
Cout<<hello;
}
};
Class B:public A
{
Public:
B()
{
Cout<<const of B;
}
};
Void main()
{
B Obj;
}
O/P: Const of A
Const of B
Class A
{
Public:
Void disp()
{
Cout<<hello;
}
};
Class B:Public A
{
Public :
B()
{
Cout<<const of B;
}
};
Void main()
{
B obj;
}
O/P: const of B
Class A
{
Public:
A()
{
Cout<<const of A;
}
Void disp()
{
Cout<<hello;
}
};
Class B:Public A
{
Public :
Void show()
{
Cout<<hello friends;
}
};
Void main()
{
B obj;
}
O/P: const of A
Class A
{
Public:
A(int x)
{
Cout<<the value of x=<<x;
}
};
Class B:Public A
{
Public :
Void show()
{
Cout<<hello friends;
}
};
Void main()
{
B obj; //B obj(25);
}
O/P: ERROR
Class A
{
Public:
A(int x)
{
}
};
Class B:Public A
{
Public :
B():A(25)
{
}
};
Void main()
{
B obj;
}
Class A
{
Public:
A(int x)
{
}
};
Class B:Public A
{
Public :
B(int a,int b):A(a)
{
}
};
Void main()
{
B obj(12,25);
}
Ambiguity in multiple inheritance:
Class A
{
Public:
Int x;
};
Class B:public A
{
Public:
Int y;
};
Class C:public A
{
Public:
Int z;
};
Class D:public B,public C
{
Public:
Void disp()
{
X=90;.//error
Cout<<value=<<x;.//error
}
};
Void main()
{
D obj;
Obj.disp();
}
So,
X=90 will produce error we can correct them
by using:
1.)
Class A
{
Public:
Int x;
};
Class B:public A
{
Public :
Int y;
};
Class C :public A
{
Public:
Int z;
};
Class D:public, C,B
{
Public:
Void disp()
{
B::x=90;
Cout<<value=<<B::x;
}
};
Void main()
{
D obj;
Obj.disp();
}
2.)
By using virtual Base class:
Class A
{
Public:
Int x;
};
Class B:public virtual A
{
Public:
Int y;
};
Class C:public virtual A
{
Public:
Int z;
};
Class D:public B,public C
{
Public:
Void disp()
{
X=90;
Cout<<value=<<x;
}
};
Void main()
{
D obj;
Obj.disp();
}
Class A
{
Int x,y;
Public:
Void input()
{
Cout<<enter two no.;
Cin>>x>>y;
}
Void disp()
{
Z=x+y;
}
};
Void main()
{
A *obj;
Obj=new A;
Obj->input();
Obj->disp();
Delete obj;
Getch();
}
O/P: class A
Class A
Class A
{
Public:
Virtual Void disp()=0;
};
Class B:public A
{
Public:
Void disp()
{
Cout<<hello class B;
}
};
Class C:public A
{
Public:
Void disp()
{
Cout<<hello class C;
}
};
Void main()
{
A *obj;
Obj=new B;
Obj->disp();
Obj=new C;
Obj->disp();
Getch();
}
Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void disp()
{
Cout<<hello class B;
}
};
Void main()
{
B obj;
Obj.disp();
Obj.disp();
Getch();
}
O/P
Hello Class A
Hello class B
Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void disp()
{
Disp();
Cout<<hello class B;
}
};
Void main()
{
B obj;
Obj.disp();
Getch();
}
Class A
{
Public:
Void disp()
{
Cout<<Hello class A;
}
};
Class B:public A
{
Public:
Void disp()
{
A::disp()
Cout<<hello class B;
}
};
Void main()
{
B obj;
Obj.disp();
Obj.disp();
Getch();
}
class Shape
{
public:
virtual int getArea() = 0;
void setWidth(int w)
{
width = w;
}
void setHeight(int h)
{
height = h;
}
protected:
int width;
int height;
};
// Derived classes
class Rectangle: public Shape
{
public:
int getArea()
{
return (width * height);
}
};
class Triangle: public Shape
{
public:
int getArea()
{
return (width * height)/2;
}
};
int main(void)
{
Rectangle Rect;
Triangle Tri;
Rect.setWidth(5);
Rect.setHeight(7);
// Print the area of the object.
cout << "Total Rectangle area: " <<
Rect.getArea() << endl;
Tri.setWidth(5);
Tri.setHeight(7);
// Print the area of the object.
cout << "Total Triangle area: " <<
Tri.getArea() << endl;
return 0;
}
When the above code is compiled and
executed, it produces the following result:
Total Rectangle area: 35
Total Triangle area: 17
You can see how an abstract class defined an
interface in terms of getArea() and two other
classes implemented same function but with
different algorithm to calculate the area
specific to the shape.
Overloadable/Non-overloadableOperators:
Following is the list of operators which can be
overloaded:
+ - * / % ^
& | ~ ! , =
< > <= >= ++ --
<< >> == != && ||
+= -= /= %= ^= &=
|= *= <<= >>= [] ()
delete
-> ->* new new [] delete
[]
Following is the list of operators, which can
not be overloaded:
:: .* . ?:
class Box
{
public:
double getVolume(void)
{
return length * breadth * height;
}
void setLength( double len )
{
length = len;
}
// volume of box 3
volume = Box3.getVolume();
cout << "Volume of Box3 : " << volume
<<endl;
return 0;
}
When the above code is compiled and
executed, it produces the following result:
Volume of Box1 : 210
Volume of Box2 : 1560
Volume of Box3 : 5400
Template
Constant member:
we can declare constant variable, constant
function and constant object. A const object
can call only constant member.
2)
#include<iostream.h>
#include<conio.h>
class A
{
public:
void disp(const int a)
{
a=a+2;
cout<<"value of a ="<<a;
}
};
void main()
{
cout<<"hello ";
A obj;
obj.disp(10);
getch();
}
Friend Function:-
A friend function is a friend of a class. it can
access private member of a class with the help
of object.
#include<iostream.h>
#include<conio.h>
class A
{
int x,y;
public:
void disp()
{
cout<<"value of a =";
}
friend void abc(); // friend function
};
Type conversion:-
There are two types of type conversion
1) Implicit Type Conversion:- It is done by
compiler
2) Explicit Type Conversion:- It is done by
programmer. It is also called type
casting.
int main ()
{
int x = 50;
int y = 0;
double z = 0;
try {
z = division(x, y);
cout << z << endl;
}catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
Because we are raising an exception of type
const char*, so while catching this exception,
we have to use const char* in catch block. If
we compile and run above code, this would
produce the following result:
Division by zero condition!
OPERATOR OVERLOADING:
class student
{
Int roll_numb;
Int age;
Public:
Student(int rn,int ag)
{
Roll_numb=rn;
Age=ag;
}
Student operator +(student obj)
{
Student temp(0,0); //roll_numb and age initialized
to 0
Temp.age =age+obj.age;
Retturn temp;
}
};
Void main()
{
Student ram(1200,19),mohan(1201,20);
Student ramesh(0,0);
Ramesh=ram+mohan; // add the age of ram and
mohan and asssign the sum to the age of ramesh
Getch();
}