C
C
h>//CLASS DEMONSTRATION
class item
{ int number;
float cost;
public:
void getdata( int a, float b)
void putdata(void)
{ cout<<“number :”<<number<<“\n”;
cout<<“cost :”<<cost<<“\n”;
}
};
void item:: getdata( int a, float b)
{ number=a;
cost=b;
}
void main( )
{item x;
cout<<“\nobject x “<<“\n”;
x.getdata(100,290.5);
x. putdata( );
item y;
cout<<“\n object y”<<“\n”;
y.getdata(200,220.78);
y.putdata( );
}
object x
number:100
cost: 290.5
object y
number : 200
cost : 220.78
Nesting of Member Functions
#include<iostream.h>
class Set
{
int m, n;
public:
void input(void);
void display(void);
int largest(void);
};
int set :: largest(void)
{ if( m>=n)
return m;
else
return n;
}
void set :: input(void)
{
cout<<“Input values of m and n”<<“\n”;
cin>>m>>n;
}
void main( )
{Set s;
s.input( );
s.display( );
}
//STATIC CLASS MEMBER
#include<iostream.h>
class item
{
static int count;
int number;.
public:
void getdata(int a)
{number=a;
count++;
}
void getcount(void)
{ cout<<“count:”;
cout<<count<<“\n”;
}
};
int item :: count;
void main()
{
item a, b, c; Output Screen:
a.getcount();
b.getcount();
count:0
c.getcount(); count:0
a.getdata(100);
b.getdata(200)
count:0
c.getdata(300) After reading data
cout<<“After reading
data”<<“\n”; count:3
a.getcount(); count:3
b.getcount();
c.getcount(); count:3
}
Static Data Members
It is initialized to zero when the first object of its
class is created. No other initialization is
permitted.
Only one copy of that member is created for the
entire class and is shared by all the object of that
class, No matter how many object are created.
It is visible only within the class, but its lifetime is
the entire program.
#include<iostreamn.h>
class test
{
int code;
static int count;
public:
void setcode(void)
{ code =++count;
}
void showcode(void)
{ cout<<“Object Number:”<<code<<“\n”;
}
static void showcount(void)
{
cout<<“count:”<<count<<“\n”;
}
};
int test : : count;
void main() Output of theprogram:-
{ count:2
test t1,t2;
count:3
t1.setcode();
t2.setcode();
object number:1
test :: showcount(); object number:2
test t3; object number:3
t3.setcode();
test t3;
t3.setcode();
test :: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
}
Static Member Functions
A static member functions can have access
to only other static members( functions or
variables) declared within the same class.
class-name :: function-name;
#include<iostream.h>
class employee
{ char name[30];
float age;
public:
void getdata(void);
void putdata(void);
};
void employee :: getdata(void)
{ cout<<“Enter Name”;
cin>>name;
cout<<“Enter age”;
cin>>age;
}
Output : -
C1 = 2.5 + j1.5
C2= 1.5 + j2.5
C3= 4.0 + j4.0
Rules for Overloading Operators:-
1. Only existing operators can be overloaded.New
operators cannot be created.
2. The overloaded operator must have atleast one
operand.
3. We cannot change the basic meaning of an
operator.That is to say that we cannot redefine the (+)
operator to substract one value from the other.
4. Overloaded operators follow the syntax rules of
original operators.They cannot be overridden.
5. There are some operators that cannot be overloaded.
6. We cannot use friend functions to overload certain
operators.
However ,member functions can be used to overload
them.
Operators that cant be overloaded
Sizeof Size of operator
. Membership operator
.* Point-to-member operator
:: Scope resolution operator
?: Conditional operator
B
Multiple Inheritance:-
A derived class with several base classes.
A B
C
Hierarchical Inheritance:-
The traits of one class may be inherited by more
than one class
B C D
Multilevel Inheritance:-
The mechanism of deriving a class from another
‘derived class’.
A
C
The general form of deriving a derived class is:-
class derived-class-name : visibility-mode base-class-name
{
……….//
............// members of derived class
};
class ABC: private XYZ
{members of ABC
};
class ABC :public XYZ
{members of ABC
};
class ABC : XYZ
{members of ABC
};
Single Inheritance :PUBLIC public:-
# include<iostream.h> void mul(void);
class B void display ( );
{ int a; };
public: void B : :get_ab(void)
int b; {a = 5 ; b = 20 ;
void get_ab( ); }
void get_a(void); int B : : get_a ( )
void show_a(void); { return a ; }
}; void B : : show_A ( )
class D : public B {cout < <“A = “<<a<<“\n”;}
{ int c; void D : : mul( )
{ c = b * get_A( ) ; }
void D : : display( ) d . b = 20;
{cout<<“a = “<<get_a( )<<“\n”; d.mul( );
cout <<“b =“<<b<<“\n”; d.display( );
cout <<“c= “<<c<<“\n\n”; }
} Output:-
//---------------------------------- a=5
void main ( ) a=5
{D d; b = 10
d. get_ab( ); c = 50
d.mul( );
d.show_a( ); a=5
d.display( ); b = 20
c = 100
# include<iostream.h> void display(void);
class B };
void B : : get_ab(void)
{ int a;//private not inheritable
{cout<<“Enter a and b”;
public : cin>>a>>b;
int b; }
void get_ab( ); int B : : get_a( )
int get_a(void); { return a ; }
void show_a(void); void B : : show_a( )
{cout<<“a = “<<a<<“\n”;
};
}
class D : private B void D : : mul( )
{ int c; {get_ab( );
public: c = b* get_a( );
void mul(void); }// ‘a’ cannot be used directly
void D : : display ( ) // d . b = 20;
{ d . mul( );
show_a( ); d.display( );
cout<<“b = “<<b<<“\n”; }
cout<<“c =“<<c<<“\n\n”; Output:-
} Enter values of a and b:
5 10
void main ( ) a=5
b = 10
{
c = 50
D d; Enter values of a and b:
// d. get_ab( ); WON’T WORK 12 20
d.mul( ); a = 12
d.show_a( ); WON’T WORK b = 20
d.display( ); c = 240
Multilevel Inheritance class test : public student
class student {
{ protected:
protected: float sub1;
int roll_number; float sub2;
public: public:
void get_number(int); void get_marks(float, float );
void put_number(void); void put_marks(void);
}; };
void student::get_number(int a) void test : :get_marks(float x,float
y)
{ roll_number = a ;
{ sub1 = x ; sub2= y ; }
}
void test : : put_marks ( )
void student : : put_number( )
{cout<<“Marks in
{ cout<<“Roll Number : SUB1=“<<sub1<<“\n”;
“<<roll_number<<“\n” ;
cout<<“Marks in
} SUB2=“<<sub2<<“\n”;
}
class result : public test void main( )
{float total;//private by defaullt {result student1;
public: student1.get_number(111);
void display(void); student1.get_marks(75.0,59.5);
}; student1.display( );
void result : : display(void) }
{ Output:-
total = sub1 + sub2; Roll Number: 111
put_number( ); Marks in SUB1 = 75
put_marks( ); Marks in SUB2 = 59.5
cout<<“Total = Total = 134.5
“<<total<<“\n”;
}
Multiple Inheritance:- class P : public M , pubic N
class M {
{protected: public:
int m; void display(void);
public: };
void get_m(int); void M : : get_m(int x)
}; { m=x;}
class N
{ void N : : get_n(int y)
protected: { n= y;}
int n; void P : : display(void)
public: {
void get_n(int); cout<<“m = “<<m<<“\n”;
}; cout<<“n = “<<n<<“\n”;
cout<<“m*n =“<<m*n<<“\n”;
}
void main( )
{ P p;
p.get_m(10);
p.get_n(20);
p.display( );
}
Output:-
m = 10
n = 20
m*n = 200
Resolving Ambiguity in void main( )
Single Inheritance:- {
class A B b;
{ //invokes display( ) in B
public:
b.display( );
void display ( )
{ cout << “A \n”; } //invokes display( ) in A
}; b . A : : display( );
class B : public A //invokes display( ) in B
{ b. B : : display( ).
public: }
void display( )
Output:-
{ cout << “B \n” ; }
}; B
A
B
// HYBRID INHERITANCE public :
class student void get_marks(float x ,
{ protected: float y)
int roll_number; {part1 = x ; part2 = y; }
public: void put_marks(void)
void get_number(int a ) {cout<<“Marks obtained:”<<“\n”;
{ roll_number = a ; } cout<<“Part1 = “<<part1<<“\n”;
void put_number(void) cout<<“Part2= “ <<part2<<“\n”;
{cout<<“Roll }
No:”<<roll_number<<“\n”; } };
};
class test : public student class sports
{ { protected:
protected: float score ;
float part1, part2;
public : void result : : display(void)
void get_score(float s) {total = part1+part2+score;
{score = s ; } put_number( ) ;
void put_score(void) put_marks( );
{cout<<“Sports wt put_score( );
:”<<score<<“\n”; cout<<“Total
} Score:”<<total<<“\n”;
}; }
class result : public test, public void main( )
sports { { result student_1;
float total ; student_1.get_number(1);
public: student_1.get_marks(27.5,33.0);
void display(void); student_1.get_score(6.0);
}; student_1.display( ) ;
}
Output:-
Roll No: 1
Marks Obtained :
Part1:27.5
Part2:33
Sports wt : 6
Total Score:66.5
Constructors in derived class:-
# include<iostream .h> class beta
class alpha { float y ;
{
public:
int x;
public:
beta( float j )
alpha( int i) {
{ y=j;
x=i; cout<<“beta initialized”;
cout<<“alpha initialized \n”; }
} void show_y(void)
void show_x(void) { cout<<“y = “<<y<<“\n”; }
{ cout<<“x = “<<x<<“\n”; }
};
};
class gamma : public beta, void show_mn(void)
public alpha {
{ int m ,n ; cout<<“m = “<<m<<“\n”;
public: <<“ n = “<<n<<“\n”;
gamma( int a , float b , int c }
int d ) : alpha (a) , beta(b) };
{ void main( )
m = c; {
n = d; gamma g(5 ,10.75,20,30);
cout<<“gamma initialized cout<<“\n”;
\n “; g.show_x( );
} g.show_y( );
g.show_mn( );
}
Output:- constructor(arglist) :
beta initialized initialization section
alpha initialized {
gamma initialized assignment-section
}
x=5
y = 10.75
m = 20
n = 30
/* C++ supports another
method of initializing class
objects. This method uses
what is called initialization
list in the constructor function.
INITIALIZATION LIST IN class beta
CONSTRUCTORS { float p , q ;
# include<iostream.h> public :
class alpha beta (float a, float b) :
{ p(a) , q(b+p)
int x;
{
public:
cout<<“\n beta constructed”;
alpha( int i)
}
{ x=i;
void show_beta(void)
cout<<“alpha initialized \n”;
{
}
cout<<“ p = “<<p<<“\n”;
void show_x(void)
cout<<“ q = “<<q<<“\n”;
{ cout<<“x = “<<x<<“\n”; }
}
};
};
class gamma :public beta , void main( )
public alpha {
{int u , v ; gamma g(2, 4 , 2.5);
public : cout<<“\n\n Display
gamma(int a , int b, float c) : member values “;
alpha(a*2),beta(c , c) ,u(a) g.show_alpha( );
{v = b; g.show_beta( );
cout<<gamma constructed”; g.show_gamma( );
} }
void show_gamma(void)
{cout<<“u = “<<u<<“\n”;
cout<< v = “ <<v<<“\n”;
}
};
Output:- /* The argument list in the
beta constructed derived constructor gamma
alpha constructed contains only three
gamma constructed parameters a, b and c
Display member values which are used to initialize
x =4 five data members
p = 2.5 contained in all the three
q =5 classes.*/
u =2
v =4