0% found this document useful (0 votes)
1K views74 pages

C

The document discusses C++ classes including class definitions, member functions, constructors, destructors, static members, friend functions, and copy constructors. It provides examples of declaring classes with data members and member functions, defining constructors and destructors, declaring static member variables and functions, passing objects as arguments to friend functions, and using copy constructors. The examples demonstrate creating objects, accessing class members, and displaying output to illustrate the concepts.

Uploaded by

xblueknight
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views74 pages

C

The document discusses C++ classes including class definitions, member functions, constructors, destructors, static members, friend functions, and copy constructors. It provides examples of declaring classes with data members and member functions, defining constructors and destructors, declaring static member variables and functions, passing objects as arguments to friend functions, and using copy constructors. The examples demonstrate creating objects, accessing class members, and displaying output to illustrate the concepts.

Uploaded by

xblueknight
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 74

#include<iostream.

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 set:: display(void)


{
out<<“Largest value=“<<largest()<<“\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.

A static member function can be called using the


class name( instead of its objects) as follows

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;
}

void employee ::putdata(void)


{
cout<<“Name: ”<<name<<“\n”;
cout<<Age: ”<<age<<“\n”;
}
const int size=2;
void main()  Output
{  Details of Manager 1
employee manager[size]; Enter name:XXX
for(int i=0,i<size;i++) Enter age:45
{ Details of Manager 2
cout<<“Details of Enter name:YYY
Manager”<<i+1<<endl; Enter age:35
manager[i].getdata();  Manager 1
} Name: XXX
cout<<endl; Age: 45
for(int i=0,i<size;i++) Manager 2
{ Name: YYY
cout<<“Manager”<<i+1<<endl; Age: 35
manager[i].putdata();
}
}
//Passing Objects as Coordinates
#include<iostream.h>
class time
{int hours,minutes;
public:
void gettime(int h, int m)
{hours=h; minutes=m;}
void puttime(void)
{ cout<<hours<<“hours and”;
cout<<minutes<<“minutes “<<endl;
}
void sum(time,time);
};
void time :: sum(time t1,time t2)
{minutes=t1.minutes+t2.minutes;
hours=minutes/60;
minutes=minutes%60;
hours=hours+t1.hours+t2.hours;
void main() Output:
{ T1 = 4 hours 45 minutes
time T1,T2,T3; T2 = 3 hours 30 minutes
T1.gettime(4,45); T3 = 8 hours 15 minutes
T2.gettime(3,30);
T3.sum(T1,T2);
cout<<“T1 = “;
T1.puttime( );
cout<<“T2 = “;
T2.puttime( );.
cout<<“T3 = “;
T3.puttime( );
}
Friendly Functions
A friend function possesses certain special
characteristics:
It is not in the scope of the class to which it
is declared as friend.
Since it is not in the scope of the class it cannot be called
using the object of that class.
It can be invoked like a normal function without the help
of any object.
Unlike member functions, it cannot access the member
names directly and has to use an object name and dot
membership operator with each member name
It can be declared either in the public or private part of a class
without affecting its meaning.
Usually, it has objects as arguments.
class sample
{ int a;
int b;
public:
void setvalue( ){a=25;b=40;}
friend float mean(sample s);
};
float mean( sample s)
{
return(s.a+s.b)/2;
}
 void main()
{
sample X;
X.setvalue();
cout<<Mean Value=“<<mean(X)<<endl;
}
Output:
Mean value=32.5
The constructor function has the following
characteristics:-
They should be declared in the public section.
They are invoked automatically when the
objects are created.
They do not have return types not even void
and therefore they cannot return values.
They cannot be inherited though a derived
class can call the base class constructor.
Like any C++ function they can have default
arguments.
Constructors cannot be virtual.
We cannot refer to their addresses.
An object with a constructor (or destructor) cannot be used as a
member of the union.
They make “ implicit calls” to the operators new and delete
when memory allocation is required.
#include<iostream.h>
class complex
{
float x, y;
public :
complex( ){ }
complex( float a ) {x=y=a;}
complex( float real , float imag )
{x = real; y = imag ; }
friend complex sum( complex , complex)
friend void show( complex);
};
complex sum( complex c1 , complex c2)
{complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return ( c3) ;
}
void show( complex c) Output:
{cout<<c. x<<“ + j”<<c. y<<endl;
} A=2.7+j3.5
B=1.6+j1.6
void main( )
{complex A(2.7,3.5);
C=4.3+j5.1
complex B(1.6);
complex C; P=2.5+j3.9
C=sum( A,B);
cout<<“A= “; show(A); Q=1.6+j2.5
cout<<“B= “; show(B); R=4.1+j6.4
cout<<“C= “; show(C);
complex P,Q,R;
P=complex(2.5,3.9);
Q=complex(1.6,2.5);
R=sum(P,Q);
cout<<endl;
cout<<“P = “; show(P);
cout<<“Q = “; show(Q);
cout<<“R = “; show(R);
}
Copy Constructor

It is used to declare and initialize an object


from another object

A reference variable is used as an argument to the copy


constructor. We cannot pass the argument by value to the
copy constructor

When no copy constructor is defined compiler calls


its own copy constructor.

The process of initializing through a copy constructor is


called copy initialization.
# include<iostream.h> void main()
class code { code A(100);
{ code B(A);
int id; code C = A;
public: code D;
code( ) { } D=A;
code( int a) cout<<“\n id of A : ” ; A.display( ) ;
{ id = a; cout<<“\n id of B : ” ; B.display( ) ;
} cout<<“\n id of C : ” ;C.display( ) ;
code( code & x) cout<<“\n id of D : ” ;D.display( ) ;
{ id = x.id ; }
} Output:-
void display (void) id of A : 100
{cout<<id; id of B : 100
} id of C : 100
}; id of D : 100
Destructor:-
A destructor as the name implies is used to destroy the
objects that have been created by a constructor.

A destructor never takes any argument nor does it return any


value

It will be invoked implicitly by the compiler upon exit from the


program(or block or function as the case may be) to clean up
storage that is not accessible.

It is a good practice to declare destructors in a program as it


releases memory for future use.
# include <iostream.h>
int count = 0;
class alpha
{
public :
alpha( )
{
count++ ;
cout<<“\n SNo .of object created “ <<count;
}
~alpha( )
{
cout<<“\n SNo .of object destroyed”<<count;
count--;
}
};
void main( ) Output:-
{ ENTER MAIN.
SNo .of object created 1
cout<<“\n \n Enter Main\n”;
SNo .of object created 2
alpha A1, A2 , A3 , A4 ; SNo .of object created 3
{ SNo .of object created 4
cout<<“ Enter BLOCK1”; Enter BLOCK1
alpha A5; SNo .of object created 5
} SNo .of object destroyed 5
Enter BLOCK2
{cout<<“\n Enter BLOCK2”;
SNo .of object created 5
alpha A6; SNo .of object destroyed 5
} Re-enter Main
cout<<“\n Re-enter Main”; SNo .of object destroyed 4
} SNo .of object destroyed 3
SNo .of object destroyed 2
SNo .of object destroyed 1
# include<iostream.h>
class class2; // Known as forward declaration
class class1
{
int value1;
public:
void indata (int a) { value1=a;}
void display (void) { cout <<value1<<“\n”;}
friend void exchange(class1 & , class2 &);
};
class class_2
{ int value2;
public:
void indata (int a) { value1=a;}
void display (void) { cout <<value1<<“\n”;}
friend void exchange(class1 & , class2 &);
};
void exchange(class1 & x , class2 & y ) C1.display( );
{ C2.display( );
int temp = x.value1; }
x.value1= y.value2; Output:-
y.value2 =temp;
Values before
}
void main( )
exchange:
{class_1 C1; 100
class_2 C2; 200
C1.indata(100); Values after
C2.indata(200); exchange
cout<<“Values before exchange”<<“\n”;
200
C1.display( );
C2.display( );
100
exchange(C1 , C2);
Assignment:-
Programs 5.1 to 5.6(Debugging exercises
do in the assignment copy)

Program 5.1 5.2,5.5,6.2 and 6.3(Programming


exercises take printout of programs and output)
Defrencing Operators:-
void main( )
# incude<iostream.h>
{
class M
M n;
{ int x;
void ( M : : * pf)(int,int)=&M :
int y; :setxy;
public: (n.*pf) ( 10 , 20);
void set_xy( int a , int b ) cout<<“SUM=“<<sum(n)<<“\n”;
{ x = a; y = b ; } M * op = &n;
friend int sum( M m ); (op->*pf)( 30, 40);
}; cout<<“SUM=“<<sum(n)<<“\n”;
int sum( M m) }
{ int M : : * px = & M : : x;
int M : : * py = & M : : y; Output:-
M * pm = &m; sum = 30
int S = m.*px + pm->*py; sum = 70
return S;
}
Constructors
They should be declared in public section.
They are invoked automatically when the objects are created.
They do not have return types, not even void and therefore,
they cannot return any values.
They cannot be inherited , though a derived class can call the
base class constructor.
Like any C++ functions , they can have default arguments.
Constructors cannot be virtual.
We cannot refer to addresses.
An object with a constructor ( or destructor ) cannot be used
as a member of union.
They make ‘implicit calls’ to the operators new and delete
when memory allocation is required.
When a constructor is declared for a class ,initialization of the
class objects become mandatory
Overloaded Constructors
#include<iostream.h>
class complex
{
float x, y;
public :
complex( ) { } // Constructor with no arguments
complex( float a ) { x = y = a ; } // Constructor with no arguments
complex( float real , float imag )// Constructor with two arguments
{ x = real ; y = imag ; }
friend complex sum (complex,complex);
friend void show(complex);
};
complex sum (complex c1 , complex c2)
{ complex c3;
c3.x=c1.x+c2.x;
c3.y=c1.y+c2.y;
return (c3);
}
void show( complex c)
{
cout<<c .x<<“+j”<<c . y<<“\n”;
}
void main( ) cout<<“\n”;
{complex A ( 2.5, 3.5); cout<<“ P = “;show (P);
complex B(1. 5 ); cout<<endl;
complex C; cout<<“ Q = “;show (Q);
C = sum (A , B); cout<<endl;
cout <<“A = “ ;show( A ) ; cout<<“ R = “;show (R);
cout<<endl; cout<<endl;
cout <<“B = “ ;show( B ) ; }
cout<<endl; Output:-
cout <<“C = “ ;show( C ) ; A = 2.5+j3.5
complex P,Q,R ; B = 1.5+j1.5
P=complex(2.5,3.0); C= 4.0+j 5.0
Q=complex(1.5,2.0); P=2.5+j3.0 ;
R=sum( P, Q); Q=1.5+j2.0 ;
cout<<“\n”; R=4.0+j5.0
Operator Overloading
It allows to attach additional meaning to
operators.
The general form of an operator function is:-
return_type class_name: : operator symbol(argument list)
{
// task defined
}
The function operator op( ) is alwaus written in the public part of
the class.It may be either a member function or a friend function
Overloading Unary Minus
void space : :getdata(int a,int b,int c)
# include<iostream.h> {x=a;
class space y=b;
{ int x; z=c;
int y; }
int z; void space : : display (void)
public: {cout<< x <<“ ”;
void getdata(int a, int b,int c); cout<< y << “ “;
void display( void ); cout<< z <<“ “ ;
void operator – ( ); }
}; void space : : operator-( )
{x = - x;
y= - y;
z= -z; }
void main( )
{space S;
S.getdata(10,-20,40);
cout << “S : “;
S.display();
- S ; // activates operator- () function or S -
cout<< “ S : “;
S.display( ) ;
}
Output:-
S : 10 -20 40
S : -10 20 -40
Overloading Binary + operator
#include<iostream.h> complex complex : : operator +
class complex ( complex c);
{ float x, y; {complex temp;
public : temp . x= x + c. x;
complex( ) { } temp . y= y + c .y;
complex( float real , float imag ) return (temp);
{ x = real ; }
y = imag ; void complex : : display(void)
} {
complex operator +(complex); cout<<x<< “ + j” <<y << “\n”;
void display(void); }
};
void main ( )
{
complex C1 , C2 ,C3;
C1 = complex(2.5 , 1.5);
C2 = complex(1.5 , 2.5);
C3=C1+C2;
cout<< “C1 = “; C1.display( ) ;
cout<< “C2 = “; C2.display( ) ;
cout<< “C3 = “; C3.display( ) ;
}

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

Where a friend cannot be used


= Assignment operator
() Function call operator
[] Subscripting operator
-> Class member access operator
7. Unary operators overloaded by means of a member function
take no explicit arguments and return no explicit values, but
those overloaded by means of a friend function ,take one
reference argument (the object of the relevant class).

8. Binary operators overloaded through a member function take


one explicit argument and those which are overloaded through
a friend function take two explicit arguments.

9. When using binary operators overloaded through a member


function the left hand operand must be an object of the relevant
class.

10.Binary arithmetic operators such as + , - , * , / must explicitly


return a value.They must not attempt to change their own
arguments.
Type Conversions
// Basic to Class Type
class Time
{ int hrs, mins;
public:
………………….
time( int t)
{/* The constructors used for type conversion take a single
argument whose type is to be converted.*/
hrs = t / 60;
mins = t% 60;
}
};
time T1;
int duration = 85;
T1=duration;
//Class to Basic Type
vector : : operator double( ) //Casting Operator Function
{double sum = 0;
for( int i = 0 ; i < size ; ++i)
{ sum = sum +v[ i ] * v [i ] ;
}
return sqrt(sum);
}
double length = double(V1); or double length = V1;
Here V1 is an object of type vector
The casting operator function should satisfy following conditions:-
It must be a class member.
It must not specify a return type.
It must not have any arguments.
One Class to Another Class void putdata( )
Type {cout<<“Code : “<<code<<“\n”;
# include<iostream.h> cout<<“Items:”<<items<<“\n”;
class invent2 cout<<“Value:”<<price<<“\n”;
//Destination class declared }
class invent1 // Source class int getcode( ) { return code ; }
{ int getitems( ) { return items;}
int code; float price( ) { return price; }
int items; operator float( )
float price; {return (items * price ) ; }
public : /* operator invent2 ( )
invent1(int a, int b , float c) {// invent1 to invent2
{code = a ; invent2 temp;
items = b ; temp . code = code ;
price = c ; temp . value = price * items;
} return temp ;
}*/
};
class invent2 invent2(invent1 p)
{ int code; {//constructor for conversion
float value; code = p . getcode( );
public : value = p.getitems( ) *
invent2( ) p.getprice( );
{code = 0; value = 0; } }
invent2( int x ,float y ) };// End of destination class
{code = x; void main ( )
value = y; { invent1 s1(100,5,140.0 );
} invent2 d1;
void putdata( ) float total _value;
{cout<<“Code:”<<code<<“\n”; //invent1 To float
cout<<“Value:”<<value<<“\n”; total_value = s1;
} /* invent1 To invent2*/
d1 = s1;
cout<<“Product details –
invent1 type”<<“\n”;
s1.putdata () ;
cout<<“Product details-invent2
type”<<“\n”;
d1. putdata( ) ;
}
Output:-
Product details-invent1
type
Code: 100
Items: 5
Value: 140
Stock value
value = 700
Product details – invent2
type
Code: 100
Value: 700
void main( )
Object Pointers {
item abc;
class item
item *ptr = &abc;
{ int cd;
/* ptr is a pointer object of
float cost;
item class that is initialized
public:
with the address of another
void getdata( int x , float y)
object abc.*/
{ cd = x;
abc.getdata(26 , 150.50);
cost = y;
abc .show( );
}
Or
void show(void)
{cout<<“Code:”<<cd<<“\n”
ptr->.getdata(26 , 150.50);
<<“Price:”<<cost<<“\n”; ptr-> .show( );
} (*ptr).getdata(26 , 150.50);
}; (*ptr) .show( );
}
Output:-
Code:26
Cost: 150.50
Code:26
Cost: 150.50
Code:26
Cost: 150.50
# include<iosream.h> while (y< = t)
void main( ) {sum = sum + sum *r;
{float amount; y = y + 1;
float value(float p, int t, }
float r = 0.15) return sum ;
amount = value(500.00, 5); }/*Default arguments are
//default for 3rd argument useful when some
} argumets always have the
float value(float p, int t, same value .We must add
float r ) defaults from right to left
{int y = 1; We cannot provide a
float sum = p; default value in the middle
/*Default values are specified of argument list.
when the function is declared*/
Inheritance : Extending Classes
Reusability is yet another feature of OOP.
It is always nice if we could reuse something
that already exist rather than trying to create
the same all over again.
It will not only save time and money but also
reduce frustration and increase reliability
Once a class has been written and tested ,it can
be adopted by other programmers to suit their
requirements.
This is basically done by creating new
classes, reusing the properties of existing ones.
The mechanism of deriving a new class from an
old one is called inheritance (or derivation).
The old class is referred to as the base class and
the new one is called derived class or subclass.
Single Inheritance:-
A derived class with only one base class

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy