CPP Notes
CPP Notes
I INTRODUCTION TO C ++:
1. C ++ is considered as a superset of ‘C’.
2. C ++ is also known as “c with classes”
3. In earlier of 1980’s Bjarne stroustrup is the person who introduced C
++ at AT & T bells of U.S.A.
Include files
Class declaration
Main function
Cin
1 Cout 2
All the C++ programs should be saved with .CPP as the extension
name.
Cout (printf)
1. Cout is a object
2. Console out < < is known as insertion operator which is used to display
the string or variable values to the screed through the cout object
Object insection
<<
C++
Cout
Variable
strings
Monitor
>>
C++
Cin
Variable
strings
Keyboard
eg: int a;
cout < < “enter a value ”;
cin > > a ;
cin > > a > >b;
2. # include <iostream.h>
# include <conio.h>
void main ( )
{
clrscr ( );
cout < < “ Welcome \n”;
cout < < “ to \n”;
cout < < “ K c I \n”;
getch ( );
}
O/p welcome
To
Kci
2nd method
cout < < “welcome” < <” to” < < “kci”;
O/p welcome to kci
Cout < <”Welcome \t” < <” to \t” < < “Kci”;
O/p Welcome to kci
%d %f (cout of string)
2. # include <iostream.h>
# include <conio.h>
void main ( )
{
int m;
clrscr ( );
cout < < “ Enter m value \n”;
cin > > “ \n\n” < < m;
cout < < “\n\n m = “< <m;
getch ( );
} O/p enter m value 3000 3000 m = 3000
3. ADDITION OF TWO NUMBERS:-
# include <iostream.h>
# include <conio.h>
void main ( )
{
int a,b;
clrscr ( );
cout < < “ Enter a value \n”;
cin >> a;
cout < < “ Enter b value \n”;
cin >> b;
cout << “\n a = “<< a;
cout << “\n b = “<< b;
cout << “\n sum = “<< a+b;
getch ( );
}
O/p enter a value 45
Enter b value 35
a = 45
b = 35
sum = 80
4. # include <iostream.h>
# include <conio.h>
void main ( )
{
int a = 500, b = 700;
clrscr ( );
cout < < “\n\n a = “<< a << “\n\n b= “ <<b”;
getch ( );
}
O/p a= 500
B = 700
2nd method
cout <<”\n\n a= “<< a<<” \n\n” << “b=”<<b;
O/p a = 500
B = 700
Assignment
1. Factorial
# include <iostream.h>
# include <conio.h>
void main ( )
{
long int n, i=1, f=1;
clrscr ( );
cout < < “ Enter n value \n”;
cin >> n;
while (i<n)
{
f = f x I;
i++;
}
cout < < “ factorial = “<<f;
getch ( );
}
O/p enter n value
5
factorial = 120
enter n value
8
factorial = 4030
enter n value
12
factorial = 479001600
2. Reverse:-
# include <iostream.h>
# include <conio.h>
void main ( )
{
int r,s = o,n;
clrscr ( );
cout < < “ Enter n value \n”;
cin >> n;
do
{
r = n%10;
s = (s * 10) + r;
n = n/10;
cout < < “ \n\n reverse of a given number =”<< s;
getch ( );
}
O/p:-
Enter n value
123
reverse of a given number = 321
(or)
enter n value
12345
reverse of a given number = -11215
because if reverse
54321
11/Oct/08
1. Program to accept serial number, name and average and print them.
# include <iostream.h>
# include <conio.h>
void main ( )
{
int s.no;
char name [10];
float average;
clrscr ( );
cout < < “ Enter s.no \n”;
cin >> s.no;
cout < < “ Enter name \n”;
cin >> name;
cout << “\n a = “<< a;
cout < < “ Enter average \n”;
cin >> average;
cout < < “ given details \n”;
cout << “ s.no = “<< s.no << “\n\n”;
cout << “ name = “<< name << “\n\n”;
cout << “ average = “<< average << “\n\n”;
getch ( );
}
O/p enter s.no 2011
Enter name sapna
Enter ave. 89.9
Given details
s.no. = 2011, name = sapna, average = 89.9
H.w
Matix m * n
Functions
Structures
FUNCTIONS H.W.
1. Without return type & without parameters
# include <iostream.h>
# include <conio.h>
void sapna ( ) => function definition
void main ( )
{
clrscr ( );
cout <<”\n\n hai \n”;
sapna ( ); fun calling
sapna ( );
cout << “ \n bye ”;
getch ( );
}
void sapna ( ) => function definition
{
cout << “how r u \n”;
}
O/p
Hai
How r u
How r u
Bye
2. # include <iostream.h>
# include <conio.h>
struct student
{
int s.no;
char name [45];
float avg;
};
void main ( )
{
struct student p = {1001, “sapna”, 98.93};
clrscr ( );
cout << “\n sizeof (m)”;
cout <<”\n\n “ << p s.no. << p. name << p. avg;
getch ( );
}
3. Accessing structure add (a,b,c); variable and print them using “scanf”
# include <iostream.h>
# include <conio.h>
struct sameera
{
int s.no;
char name [25];
float avg;
}
void main ( )
{
struct sameera u;
cout << “enter s.no., name, avg \n”;
cin >> u.sno >> u. name >. U. avg;
cout << “\n\n given details :\n\n”;
cout < < “ \n s.no = “<< u.sno.;
cout < < “ \n name = “<< u. name.;
cout < < “ \n avg = “<< u. avg.;
cout << “\n “, sizeof << t;
getch ( );
}
1. OBJECT:
Object is a entity which is used to store, access date of the class.
Object is considered as an instance of the class
2. CLASSES:
Class is a key word
Class is a way to bind (combine)
Variables and functions together
Variables are known as data members (Private)
Functions are known as member functions (Public)
Declaration of a class:-
Class classname
{
private:
data members declarations;
member functions declaration;
public;
data members declarations:
members functions declaration / definition
};
A class is divided into two sections private section and public section.
Class is a entity to combine data members and member functions
together
Private part should be accessed within the class only
Public part can be accessed within the class and outside the class
also.
Private, Public are the key words.
Extension of structures is considered is a class
COMPLEX PROGRAM:-
# include <iostream.h>
# include <conio.h>
class complex
{
private:
int a,b;
public:
void set ( )
{
cout << “\n enter seal part \n”;
cin>>b;
}
void put ( )
{
cout <<”\n <<a<<”+i”<<b;
cout <<”\n <<a<<”+i”<<b;
}
};
void main ( )
{
clrscr ( );
complex x;
x.set ( );
x.put ( );
getch ( );
}
O/p enter real part
3
enter imaginary part
5
3+i5 // 3/5
# include <iostream.h>
# include <conio.h>
class complex
{
private:
int m,n;
public:
void input ( )
{
cout<<”enter 2 values \n”;
cin >> m>>n
}
void show ( )
{
cout <<”\n\n complex no.:”<<m<<”+i"<<n;
cout <<”\n\n creational no.:”<<m<<”\n"<<n;
}
};
void main ( )
{
complex s;
clrscr ( );
s.input ( );
s.show ( );
getch ( )
}
O/p
Enter 2 values
2
3
complex no: 2+i3
rational no : 2/3
II. PASSING PARAMETERS (ARGUMENTS) TO THE MEMBERS FUNCTIONS:-
# include <iostream.h>
# include <conio.h>
class sachin;
{
int a,b;
public;
void set (int x, int y)
{
a=x, b=y;
}
void show ( )
{
cout << “\n\n a=”<<a;
cout << “\n\n b=”<<b;
cout <<”\n\n mul of a &b = “<<a*b;
}
};
void main ( )
{
sachit t;
clrscr ( );
t.set (10,20);
t. show ( );
getch ( );
}
O/p
A=10
B=20
Mul of a&b = 200
2nd method
void main ( )
{
sachin t1,t2,t3;
clrscr ( );
t1.set (5,6);
t2.set (40,80);
t3.set (-5, -10);
t1.show ( );
t2. show ( );
t3. show ( );
getch ( );
}
O/p
a=5 b=6
mul of a & b = 30
a =40 b=80
mul of a&b = 3200
a=-5 b=-10 mul of a &b = 50
3. DEFINING MEMBER FUNCTIONS OUTSIDE THE CLASS:
DECLARATION:-
Return type classname:- functionname ( )
{
// member function body
}
# include <iostream.h>
# include <conio.h>
class sample
{
int a,b;
public:
void input ( );
void show ( );
};
void sample :: input ( )
{
cout <<”\enter any 2 values\n”;
cin >> a >>b;
}
void sample; show ( )
{
cout <<”\a =”<<a;
cout <<”\n\n b=”<<b;
}
void main ( )
{
sampl s;
clrscr ( );
s. input ( );
s. show ( );
getch ( );
}
O/p
Enter any 2 values
2
3
a=2
b=3
2. Circle:-
# include <iostream.h>
# include <conio.h>
class circle;
{
float r;
public:
void set (float m);
{
r =m;
}
void display ( );
};
void circle :: display ( )
{
court << “\n r = “<< r;
cout << “\n area = “<< 3.14 * r * r;
cout << “\n perimeter = “<< 2 * 3.14 * r;
}
void main ( )
{
circle c1, c2;
clrscr( );
c1.set (4,5);
c2.set (12, 3);
c1. display ( );
c2. display ( );
getch ( );
}
O/p
R=4.5 Area = 63.585
Perimeter = 28.26
R = 12.3 area = 475.050615
perimeter = 77.244001
15/oct/08 MEMBER FUNCTIONS WITH RETURN TYPE
# include <iostream.h>
# include <conio.h>
class sample
{
int a,b; => data members
public:
void set (int x, int y) => member functions
{
a=x, b=y;
}
int add ( ); member fun. With return type
int sub ( );
void show ( ); => member function
};
int sample :: add ( )
{
return (a+b);
}
int sample :: sub ( )
{
return (a-b);
}
void sample :: show ( )
{
cout <<”\n a=” <<a;
cout <<\n b =” <<b;
cout << “\n add = “<< add ( );
cout << “\n sub = “<< sub ( );
}
void main ( )
{
sample t;
clrscr ( );
t.set (8,3);
t. show ( ); // cout <<”\n add = “<< t.add ( );
getch ( ) // cout <<”\n sub = “<< t.sub ( );
}
O/p 2nd method
a=8 a=8
b=3 b=3
add=11 add=11
sub=5 sub=5
2. Define a class circle accept radius calculate area and perimeter and print
with return type
# include <iostream.h>
# include <conio.h>
class circle
{
float r;
public:
void set (float s)
{
r=s;
}
float area ( )
{
return (3.14 * r * r );
}
float peri ( )
{
return (2 * 3.14 * r);
}
};
void main ( )
{
circle a;
clrscr ( );
a.set (7.5);
cout << “\n radius =”<<a.r;
cout << “\n area =” << a.area ( );
cout <, “\n peri =”<< a.peri ( );
getch ( );
}
O/p
Radius = 7.5
Area = 176.625
Perimeter = 47.099998
# include <iostream.h>
# include <conio.h>
class complex
{
int a,b;
public:
void set (intx, inty)
{
a=x, b=y;
}
void show ( )
{
cout <<”\n\n”<<a<<”+i"<<b;
}
void add (complex c1, complex c2)
{
a=c1.a+c2.a;
b=c1.b+c2.b;
} => void sub (complex c1, complex c2)
}; {
void main ( ) a=c1.a - c2.a;
{ b=c1.b - c2.b;
complex s1,s2,s3 }
clrscr ( ); };
s1.set (4,5);
s2.set (6,7);
s3.add (s1, s2); => s4. sub (s1,s2);
s1.show ( );
s2.show ( );
s3. show ( ); // cout<< “\n add :”; => s4. show ( );
getch ( );
{
O/p
1. Addition
4+i5
6+i7
10+i12
2. Add/sub
4+i5
6+i7
10+i12
-2+i-2
# include <iostream.h>
# include <conio.h>
int add (int, int);
float add (float, float);
float add (int, float);
void main ( )
{
clrscr ( );
cout <<”\n\n\t add of 2 I’s =”<, add (10,20);
cout << “\n\n\t add of 2 float values = “<< add (12.5, 16.4);
cout <<”\n\n\t add of 2 no’s = “<< add (25, 45.11);
getch ( );
}
int add (int x, int y);
{
return (x+y);
}
float add (float p, float q);
{
return (p+q);
}
float add (int s, float m);
{
return (s+m);
}
O/p ambiguity b/w add(int, int) ad “add/int, float
1. Pgm.
# include <iostream.h>
# include <conio.h>
int area (int);
int area (int, int);
double area (double);
void main ( )
{
Clrscr ( );
cout <<”\n\t area of c = “<< area (7, 5);
cout <<”\n\t area of s = “<, area (8);
cout <<”\n\t area of r = “<< area (5,6);
getch ( );}
int area (int m)
{
Return (m * m);
}
int area (int l, int b)
{
return (l * b);
}
Double area (double r)
{
return (3.14 * r * r);
}
O/p area of c= 176.625
Area of s = 64
Area of r = 30
FRIEND FUNCTION:-
Friend function:-
A non member function cannot access private data of the class.
To access private data of the class we can define a member function
as a friend function.
By using friend functions we can access the data from more than one
class.
Friend is a keyword used.
A friend function should be declared within the class and should be
defined outside the class.
In friend function only objects has to be passed as parameters.
A friend function should be called as a normal function without using
any object
# include <iostream.h>
# include <conio.h>
class first
{
private:
int a,b;
public:
void set (int x, int y);
{
a =x, b=y;
}
void show ( );
{
cout << “\n\n a=”<< a;
cout << “\n\n b=”<< b;
}
friend float mean (first);
};
float mean (first f)
{
float m;
m=(f.a+f.b);
return (m);
}
void main ( )
{
first s;
clrscr ( );
s.set (6,7);
s.show ( );
cout <<”\n\n mean :”<< mean (s)
getch( );
}
O/p
a=6
b=7
mean : 6.5
2. Maximum of 2 numbers:-
# include <iostream.h>
# include <conio.h>
class second;
class first
{
int a;
public:
void set a (int x);
{
a=x;
}
void show a ( )
{
cout <<”\n\n a=”<<a;
}
friend void max (first , second);
};
class second
{
int b;
public:
void set b (int y)
{
b = y;
}
void show b ( );
{
cout <<”\n\n b=”<<b;
}
friend void max (first, second);
};
void max (first, second);
{
if (f.a>s.b);
cout << “\n max = “<<f.a;
else
cout << “\n max = “<<s.b;
}
void main ( )
{
first t;
second v;
clrscr ( );
t. set a (10);
v. set b(20);
t. show a ( );
v. show b ( );
max (t,v);
getch ( );
O/p a = 10 b = 20 max = 20.
3. Swapping of 2 numbers
# include <iostream.h>
# include <conio.h>
class second;
class first
{
int a;
public:
void set (int x);
{
a=x;
}
void show ( )
{
cout <<”\n\n a=”<<a;
}
friend void max (first f, second f);
};
class second
{
int b;
public:
void set (int y)
{
b = y;
}
void show ( );
{
cout <<”\n\n b=”<<b;
}
friend void swap (first f, second f);
};
void swap (first ff, second fs);
{
int t;
t = f.a;
f.a = s.b;
s.b = t;
}
void main ( )
{
first t;
second v;
clrscr ( );
t. set a (10);
v. set b(20);
cout <<”\n before swapping : \”n;
t. show ( );
v. show ( );
swap (t,v);
cout <<”\n after swapping : \n”;
t.show ( );
v. show ( );
getch ( );
}
O/p
Before swapping
a=10
b=20
after swapping
a=20
b=10
19/oct/2008 CONSTRUCTORS AND DESTRUCTORS
1.CONSTRUCTORS:
A constructor is a member function which is used for initialization of
data members.
Automatic initialization of variables
It is a special member function because it is having same name as its
class name.
Constructor does not have any return type.
Constructor must be declared in public section only.
TYPES OF CONSTRUCTORS:-
1. DEFAULT CONSTRUCTOR :- A constructor which does not have any parameters
(arguments)
2. PARAMETERIZED CONSTRUCTOR : A constructor which have parameters .
3. COPY CONSTRUCTOR:- A Constructor which have objects as parameters
# include <iostream.h>
# include <conio.h>
class sample
{
int a,b;
public:
sample ( );
{
a=5, b=8;
}
void show ( )
{
cout <<”\n\t a=”<<a;
cout <<”\n\t b=”<<b;
}
};
void main ( )
{
sample t;
clrscr ( );
t.show ( )
getch ( );
}
O/p
a=5
b=8
# include <iostream.h>
# include <conio.h>
class rational
{
int a,b;
public:
rational ( );
rational (int, int);
void show ( );
void add (rational, rational);
};
rational : : rational ( )
{
a=5, b=8;
}
rational : : rational (int x int y);
{
a=x, b=y;
}
void rational : : show ( )
{
cout <<”\n\n\t “<<a<<”/”<<b;
void rational :: add (rational r1, rational r2)
{
a = r1.a * r2.b + r2.a * r1.b;
b = r1.b * r2.b;
}
void main ( )
{
rational m1 (7,4), m2, m3;
clrscr ( );
m1.show ( );
m2. show ( );
m3. add (m1, m2);
m3. show ( );
getch ( );
}
O/p
7/4
5/8
76/32
Copy constructor:-
# include <iostream.h>
# include <conio.h>
class demo
{
int a;
public:
demo ( )
{
a=100;
}
demo (int x)
{
a=x;
}
demo (demo &m)
{
a = m.a
}
void show ( )
{
cout << “\n\n\t a=”<<a;
}
};
void main ( )
{
demo d1, d2 (200);
clrscr ( );
d1. show ( );
d2. show ( );
demo d3 (d1);
d3. show ( )
demo d4 = d2;
d4. show ( );
getch ( );
}
O/p
a=100
a=200
a=100
a=200
DESTRUCTORS :
1. A destructor is a member function which is used to destroy the
memory of the object created by constructor
2. A Destructor is called by itself when the scope of the object is
terminated.
3. A destructor is preceeded by a symbol tidle (~)
4. A destructor is also having same name as its class name.
5. A Destructor does not have any return type not even void.
6. A Destructor does not have any parameters.
1. Pgm
# include <iostream.h>
# include <conio.h>
class sample
{
int a,b;
public:
sample ( )
{
a=8, b=12;
}
void show ( )
{
cout <<”\n\t a = “<<a;
cout <<”\n\t b = “<<b;
}
~ sample ( )
{
show ( );
}
};
void main ( )
{
sample s;
clrscr ( );
}
O/P a=8 b = 12
2. # include <iostream.h>
# include <conio.h>
class karan
{
public :
karan ( )
{
cout <<”\n good morning“;
}
~ karan ( )
{
cout << “\n good night”;
}
};
void main ( )
{
clrscr ( );
karan r;
}
O/P good morning good night
3. Pgm
# include <iostream.h>
# include <conio.h>
Class sam
{
int a;
public:
sam ( )
{
a=10;
}
Sam (int x)
{
a=x;
}
void main ( )
{
cout <<”\n\n a=” <<a;
}
~ sam ( )
{
show ( )
}
};
void main ( )
{
sam s1;
clrscr ( );
{
sam s2 (20);
{
sam s3;
}
}
}
O/p Alt+F5
a=10
a=20
a=10
INHERITANCE (REUSE)
It is the one of the concept of object oriented programming
Reusability is a concept implemented in inheritance
Once a class is written and tested it can be adopted by other user’s to
meet their requirements
This is done by creating a new class.
The process of deriving the properties from one class to another class is
known as inheritance (derivation)
A derived class may inherit all or few properties from the base class.
Old class is known as base class, new class is known as derived class.
TYPES OF INHETANCE:-
1) Single inheritance:-
base class
A
derived class
B |
A derived class with only one base class is known as single Inheritance.
ii) Multiple Inheritance:- A derived class d with more than one base class.
Base class I
A B
base class B.C.II
d.c
iii) Herarchial Inheritance :- The properties of one base class may be inherited
by more than one derived class.
B.C
A
B C
d.cI d.c II
iv) Multilevel Inheritance :- The process of deriving the properties of base class
from already derived class
A b.c
B d.c/b.c
C d.c
v) Hybrid Inheritance :- All the 4 types of Inheritances are implemented in
hybrid Inheritance.
B C
2) by using protected
# include <iost.eam.h>
# include <ionio.h>
Class first
{
Protected:
int a;
Public;
Void set a (int x)
{
a=x;
}
};
Class second: Public first
Private error:- ‘first-set a (int)’ is not accessible
{
int b:
Public :
Void set b (int y)
{
b=y;
}
void show ( )
{
(out <<”\n\n a= “ ‘<<a;
(out <<”\n\n b = “ ‘<<b;
(out<<”\n\t a*b = “ <<a*b;
}
}
Void main ( )
{
Secondt;
Clrscr ( );
t. set a (10);
t. set b (20);
t. show ( );
getch ( );
}
o/p:-
a = 10
b = 20
a * b = 200 .
1) # include <lostream.h>
# include <conio.h>
Class first
{
Protected :
Int a ,b;
Public:
Void inputab ( )
{
Cout <<”\n\n enter a b values\n”;
(in>>a>>b;
}
};
Class second : Public first
{
Int m,n;
Public:
Void inputmn ( )
{
Count <<”\n\ enter mn values \n”;
(in>>m>>n;
}
Void showmn ( )
{
Count <<”\n a = “<<a;
Count <<”\n b = “<<b;
Count << “\n m = “<<m;
Count <<”\n n = ”<<n;
}
}
Void main ( )
{
Second s;
Clrscr ( );
S.input ab ( ) ;
S.input mn ( );
S.input mn ( );
Getch ( ) ;
}
o/p ;-
enter a b values
5
10
Enter m n values
45
78
a=5
b = 10
m = 45
n = 78
(3) # include <iostream.h>
# include <conio.h>
Class first
{
Protected:
Int a,b;
Public:
Void input ab ( )
{
cout <<”\n\n enter a b values \n”;
(in>>a>>b;
}
Void show ab ( )
{
cout <<”\n a = “ <<a:
cout <<”\n b =” <<b:
}
};
Class second : Public first
{
Int m,n;
Public :
Void input mn ( )
{
Inputabl ( );
Count <<”\n\n enter.m n values\n”;
(in>>m>>n;
}
Void showmn ( )
{
Show ab ( );
Cout <<”\n\n m = “ <<m;
Cout << “\n\n n = “ <<n;
}
};
Void main ( )
{
Second s ;
Clrscr ( );
S.input mn ( ) ;
S.Show mn ( ) ;
Getch ( ) ;
}
o/p
enter a b values
3
7
Enter m n values
5
9
a=3
b=7
m=5
n = 9.
3) # include <iostream.h>
# include <conio.h>
Class first
{
Protected :
int a,b;
Public:
Void inputab ( )
{
Cout <<”\n\n enter a b values \n”;
(in >>a>>b;
}
Void showab ( )
{
Cout <<”\n a =”<<a;
Cout <<”\n b =”<<b;
}
};
Class second : Public first
{
int m,n;
Public :
Void input mn ( )
{
Inputab ( );
Count <<”\n\n enter m n values\n”;
(in>>m>>n;
}
Void showmn ( )
{
Showab ( );
Cout<<”\n\n m = “<<m;
Cout<<”\n\n n=”<<n;
}
};
Void main ( )
{
Second S;
Clrscr ( );
S.input mn ( );
S.show mn ( ) ;
Getch ( );
}
o/p
enter a b values
3
7
Enter m n values
5
9
a=3
b=7
m=5
n=9
22 \ Oct \ 08
*MULTIPLE INHERITANCE
# include <iostream.h>
# include < conio.h>
Class first
{
Protected :
Int a,
Public :
Void set a (int x)
{
a = x;
}
};
Class second
{
Protected :
Int b;
Public :
Void set b (int y)
{
b=y;
}
};
Class third : public first, public second
{
Int c;
Public :
Void set c (int z)
{
C=z;
}
Void show ( )
{
Cout <<”\n\t a = “ <<s;
Cout <<”\n\t b = “<<b;
Cout <<”\n\t c = “<<c;
Cout <<”\n\t a*b*c = “<<a*b*c;
}
};
Void main ( )
{
Thirs t;
Clrser ( );
t.seta (5);
t.setb (8);
t.setc (10);
t.show ( ) ;
getch ( );
}
o/p:-
a=5
b=8
c=10
a * b * c = 400.
3. Multilevel
Heirarchieal Inheritance a A
# include <iostream.h> |
-------------------
# include <ionio.h> b B C c
a*b a*c.
class first
{
Protected : -> used only when it is used in second class or other class and
Int a : used only in base class.
Public :
Void set a (int x)
{
A = x;
}
};
Class second : Public first
{
Int b;
Public :
Void set b (int y)
{
B = y;
}
Void show ab ( )
{
a <-> b
Cout <<”\an\t a = “ << a;
Cout <<”\n\t b = “ << b;
Cout << “\n\t a*b = ‘ << a*b;
}
};
Class third : public first
{
Int c;
Public :
Void setc (int z)
{
C=z;
}
Void show ac ( )
{
Cout <<”\n\t a = “<<a;
Cout <<”\n\t c = “ <<c;
Cout <<”\n\t a*c “ <<a*c;
}
};
Void main ( )
{
Second s;
Third t;
Clrscr ( );
s. set a (10);
s.set b (15);
t.set a (12);
t.set c (18);
s.show ab ( );
t.show ac ( );
getch ( );
}.
o/p:-
a = 10
b = 15
a * b = 150
a = 12
c = 18
a * c =216
o/p:
a = 10
b = 15
a * b =150
a = 1262
c = 18
a * c = 22716 .
4) Multilevel Inheritance :- aA
|
# b B A*b
|
# c C a*b*c
Class first
{
Protected :
Int a ;
Public :
Void set a (int x)
{
a = x;
}
Class second : public first
{
Protected :
Int b;
Public :
Void set b (int y)
{
b = y;
}
Void mulab ( )
{
Cout <<”\n\n a *b = “ <<a*b;
}
};
Class third : public second
{
Int c;
Public :
Void set c (int z)
{
C=Z;
}
Cout <<”\n\n a*b*c = ‘<< a*b*c;
}
Void show ( )
{
Cout <<”\n\n a =”<<a;
Cout <<”\n\n b=”<<b;
Cout <<”\n\n c=”<<c;
}
};
Void main ( )
{
Thid m;
Clrscr ( );
m.set a (4);
m.set b (6);
m.set c (10);
m.show ( );
m.mulab ( );
m.mulabc ( );
getch ( );
}
o/p:-
a=4
b=6
c = 10
a*b = 24
a*b*c = 40 .
5) HYBRID INHERITANCE:-
# include <stdio.h> A
|
# include <conio.h> ------------------
B C
Class first | |
{ a*b-----------a*c
|
Protected : D
Int a;
Public :
Void set a (int x)
{
a = x;
}
};
Class second : virtual public first
{
Protected :
Int b;
Public :
Void set b (int y)
{
b = y;
}
Void mulab ( )
{
Cout <<”\n\n a*b = “<<a*b;
}
};
Class third : virtual public first
{
Protected :
Int c;
Public :
Void set c (int z)
{
C=t;
}
Void mulac ( )
{
Cout <<”\n\n a*c = “<< a*c;
}
};
Class fourth: public second, public third
{
Int d;
Public :
Void setd (int m;
{
d=m
}void mulabcdc)
{
Cout <<”abcd = “<<a*b*c*d;
}
Void show ( )
{
Cout <<”\n a = “ <<a;
Cout <<”\n b = “ <<b;
Cout <<”\n d = “<<c;
Cout <<”\n d = “<<d;
}
};
Void main ( )
{
Fourth s1;
S1. clrser ( );
S1. seta (2);
S1.setb (3);
S1.setc (4);
S1.setd (5);
S1.show ( );
S1.mulab ( );
S1.mulac ( );
Sl.mulancd ( );
Getch ( );
}
o/p:-
a * b =6 a=b
a*c=8 b=3
abcd = 120 c=4
a=2 d=5
b=3 a*b=6
c=4 a*c=8
d=5 abcd = 120
23\Oct\08 POLYMORPHISM
Key word:
VIRTUAL : - Used only in Hybrid inheritance .
Virtual is a key word.
By defining a class as a virtual vlass only a unique value can be derived
into derived class.
Duplication will not occur.
POLYMORPHISM:-
1) It is a one of the concept of (oops) object oriented programing
2) Polymorphism refers to the concept of “ one name with multiple forms”.
Polymorphism
When there is a same member function in the base class and in the
derived class is gives importance to the base class member function
only.
To overcome this problem we can define the member function as a
Virtual function.
TEMPLATES
A Template is a newly added concept to the c++
A Template is a kind of mac or o.
By using Templates we can create a group or a family of member
functions as Templates.
Once a member function is defined as a template it can be substituted
with different parameters.
Templates are of 2 types:
(a) Class Template.
(b) function Template.
Pgm
#
#
template < class t >
class sample
{
t a;
t b;
public :
void set (t x, t y)
{
a = x, b=y;
}
void show ( ).
{
cout <<”\n\nt a = “<<a;
cout <<”\n\nt b= ”<<b;
}
};
void main ( )
{
sample <int> st; o/p:- a = 10
clrscr ( ); b = 20
s1.set (10,20); a = 23.73
s1.show ( ); b = 56.849998
sample <float>s2; a=A
s2.set (23.73, 56.85); b = B.
s2.show ( );
sample <char> s3;
s3.set (‘A’, ‘B’).
s3.show ( );
getch ( );
}
cout << “ entr marks details \n”;
cout <<”enter s1 marks \n”;
cin >> s1;
cout <,”enter s2 marks \n”;
cin >> s2;
cout <<’enter s3 marks \n”;
cin >> s3;
}
};
class result : public student, public marks
{
float avg;
char grade;
int total;
public :
void show ( )
{
avy = (s1=s2=s3)/ 3;
cout << “\n total = “<<s1+s2+s3;
cout <<”\n avg = “<<avg;
If (avg > 80).
cout <<”\n grade = A”;
else
cout <<”\n garde = B”;
}
void shows ( )
{
cout <<”\n student details are : \n”;
cout <<”\name = “ << name;
cout <<”\s.no = “ << sno;
cout << “\n marks details : “;
cout <<”\n S1 = “ <<s1;
cout << “\n S2 = “ <<s2;
cout << “\n s3 = “ <<s3;
}
};
void main ( )
{
result m;
clrscr ( );
m. in put ( );
m. inpit a ( );
m.shows ( );
m.show ( );
getch ( ) ;
}
o/p:- enter student details
enter name
Sania
enter S.no.
2000
enter S1 marks
90
enter S2 marks
95
enter S3 marks
99
Student details are :
Name = Sania
S.no. = 2000
Marks details:
S1 = 90
S2 = 95
S3 = 99
Total = 284
Avg = 94
Grade = A
o/p :- enter student details
enter name
laghu
enter S.no
34
enter marks details
enter S1 marks
36
enter S2 marks
56
enter s3 marks
45
Student details are :
Nama = rag
s.no = 34
marks details :
s1 = 36
s2 = 56
s3 = 45
total = 137
avg -45
grade = B.
Function templates :-
# include <iostream.h>
# include <ionio.h>
template < class t>
void show (t x, t y )
{
cout <<”\n\n\t a = “ << x;
cout << “\n\nt b = “ << y <<” \n\n “;
}
void main ( )
{
int a =10, b =20;
clrscr ( );
chow (ab);
float m = 75.62, n = 23.71;
show (mn);
char p = ‘A’ , q = ‘B’ ;
show (p,q);
getch ( );
}
o/p:- a = 10
b = 20
a = 75.620003
b = 23.709999
a=A
b = B.