You are on page 1of 95

C ++

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.

II. STRUCTURE OF C++ PROGRAM:

Include files

Class declaration

Members functions definition

Main function

III INPUT OUTPUT FUNCTIONS IN C++


<iostream.h>
|
|

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

eg: cout < < “Karan”


cout < < “a b c “ < < “m n o”;
int a = 100;
cout < < a;
2 cin:-
1. Console in
2. cin is a object
3. > > is called extraction operator which is used to accept the values from
keyboard and store in the given variable

Object extra operation

>>
C++
Cin

Variable
strings

Keyboard
eg: int a;
cout < < “enter a value ”;
cin > > a ;
cin > > a > >b;

1. Sample program is C++


/ * my first c++ program */
# include <iostream.h>
# include <conio.h>
void main ( )
{
clrscr ( );
cout < < “ Happy Dasara \n”;
getch ( );
}
O/p Happy Dasara.

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)

3. working with variables


# include <iostream.h>
# include <conio.h>
void main ( )
{
int a= 100;
clrscr ( );
cout < < a;
cout < < “\n\n a=” << a;
getch ( );
}
O/p 100 a = 100
4. # include <iostream.h>
# include <conio.h>
void main ( )
{
int a = 100;
int b = 200;
clrscr ( );
cout < < “ \n a = << \n b = “ < <a <<n;
getch ( );
}
cout < <”a – “< < a < <”\n\n b = “< <b,
(or)
cout << “a = “ << a <<” \n\n”
< <” h = “ < <h cc
 front

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

2. Maximum of two numbers using if statement


# 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 << “ a = “<< a;
cout << “ b = “<< b;
if (a<b);
cout << “\n\n max = “ m << a;
getch ( );
}
O/p enter a value
34
Enter b value
45
a = 34
b = 45
max = 45

3. pgm to print 1 to 10 numbers


# include <iostream.h> (or)
# include <conio.h>
void main ( ) void main ( )
{ {
int a = 1; int i;
clrscr ( ); clrscr ( );
while (a<=10); for (i=1; i<=10; i++);
{ {
cout << a << “\t”; cout << i << “\t”;
a++; }
} getch ( );
getch ( ); }
}
O/p 1 2 3 4 5 6 7 8 9 10
# include <iostream.h>
# include <conio.h>
void add (int, int);
void main ( )
{
clrscr ( );
add (10,20);
add (100, 20)
getch ( );
}
void add (int m, int n)
{
add – t;
t = m+n;
cout << “\n add = “<< t;
}
O/p add = 30
add = 120

3. Pgm to accept 5 array elements


# include <iostream.h>
# include <conio.h>
void main ( )
{
int a[5], i;
cout << “enter 5 values \n”;
for (i=0; i<5; i++);
cin >> a[i];
cout << “\n\n given array elements \n”;
for (i=0; i<5; i++);
cout << “ “ << a[i];
getch ( );
}
O/p enter 5 values
1
2
3
4
1
3
given array elements
123413

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. With return type of with parameters:


# include <iostream.h>
# include <conio.h>
int add (int, float);
void main ( )
{
int p,q,r;
clrscr ( );
p = add (6, 90.0);
q = add (3,7);
r = add (5,34.3);
cout <<”\n\n add = “<<p;
cout <<”\n\n add = “<<q;
cout <<”\n\n add = “<<r;
getch ( );
}
int add (int m, float n)
{
float s;
s = m+n;
return (s);
}
O/p add = 96
add = 10
add = 39

III without return type with parameters


Add of 3 Nos.
# include <iostream.h>
# include <conio.h>
void add (int, int, int)
void main ( )
{
int a,b,c;
clrscr ( );
cout << “\n enter 3 values \n”)
cin >> a >> b >> c;
add (a,b,c);
add (a,b,c);
add (a,b,c);
add (10,20,30);
getch ( );
}
void add (int s, int p, int k);
{
int Q;
Q = s+p+k;
Cout << “\n\n add = “<<Q;
}
O/p
Enter a,b,c values
2
3
4
add 9
add 9
add 10
 max of 2 nos.
# include <iostream.h>
# include <conio.h>
void max (int p, int q);
{
if (p>q);
cout << “\n max = “ << p;
else
cout << “\n max = “ << q;
}
void main ( )
{
int a,b;
clrscr ( );
cout << “\n\n enter 2 values \n\n”);
cin > > a > >b;
max (a,b);
max (50,70);
max (0, 0);
getch ( )
}
O/p enter 2 values 2 4 max = 4 max = 70 max = 0

 FACTIONAL OF A GIVEN NUMBERS


# include <iostream.h>
# include <conio.h>
void long fact (long int);
void main ( )
{
clrscr ( );
long fact (7);
long fact (5);
long fact (3);
long fact (20);
long fact (a);
getch ( );
void fact (int m);
{
long int i, f=1;
for (i=1; i<=m; i++);
f = f*i;
cout << “\n\n long fact = “ < <f);
}
}

STRUCTURES (SIZEOF A GIVEN VARIABLE)


# include <iostream.h>
# include <conio.h>
void main ( )
{
int a;
clrscr ( );
cout << sizeof (a);
getch ( );
}
O/p
Int a;
O/p
Float a;
O/p 4
Char a;
O/p
Char a[100];
O/p 100
double a[3] = 24 long int a[10] = 40

 DEFINING STRUCTURE OUTSIDE THE MAIN FUNCTION:-


Struct sample
{
int a,b;
}
void main ( )
{
struct sample s; // struct sample s= {100, 500};
clrscr ( );
s.a=100;
s.b=500;
cout <<”\n\n a,b” << s.a <<s.b;
cout << “ “, size of (s);
getch ( );
}

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

Defining structure inside main:-


# include <iostream.h>
# include <conio.h>
void main ( )
{
struct book
{
int nop;
char b.name[20];
float cost;
} b;
clrscr ( );
cout << “\n enter book details \n”;
cin >> b.nop >> b.name >> b.cost;
cout << “\n book details are :\n”;
cout << “\n nop bname cost”, << b.nop << b. name << b.cost;
getch ( )
}
 Factional:-
# include <iostream.h>
# include <conio.h>
int fact (int)
void main ( )
{
int a,b;
clrscr ( );
cout <<”enter any number \n”;
cin >> a;
b = fact (a);
cout << “ factorial of a =” << b;
getch ( );
}
int fact (int i);
{
if (i==0)
return (1);
else
return (i*fact (i-1));
}

BASIC CONCEPTS OF OBJECT ORIENTED


PROGRAMMING OOP’S
1. OBJECT
2. CLASSES
3. DATA ENCAPSULATION
4. DATA ABSTRACTION
5. INHERITANCE
6. POLYMORPHISM
7. DYNAMIC BINDING
8. MESSAGE PASSING

I. CLASSES AND OBJECTS:-

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

I. DEFINING MEMBER FUNCTIONS INSIDE THE CLASS


# include <iostream.h>
# include <conio.h>
class sample
{
private:
int a,b;
public:
void input ( )
{
cout << “enter any 2 values \n”;
cin >> a >>b;
}
void show ( )
{
cout << “\n\n a =” << a;
cout << “\n\n b =” << b;
}
};
void main ( )
{
sample s;
clrscr ( )
s. input ( );
s. show ( );
getch ( );
}
O/p enter any 2 values
25 45 a = 25
b = 45
2. BY DEFAULT ALL THE DATA MEMBERS ARE PRIVATE
# include <iostream.h>
# include <conio.h>
class karan
{
//public:
int a,b;
void set ( )
{
cout << “\n enter 2 values \n”;
cin > > a > >b;
}
};
void main ( )
{
karan t;
clrscr ( );
t. set ( );
cout <<”\n\n a =” << a;
cout <<”\n\n b =” << b;
getch ( );
}
3. DEFINE A CLASS CIRLCE ACCEPT RADIOUS CALCULATE AREA AND
PERIMETER AND PRINT THE DETAILS:-
# include <iostream.h>
# include <conio.h>
class circle;
{
float r;
public:
void getdate ( )
{
cout <<”enter radius \n”;
cin >>r;
}
void display ( )
{
cout << “\n\n radius =” <<r;
cout <<”\n area =” << 3.14 *r*r;
cout << “\n perimeter =”<< 2*3.14*r;
}
};
void main ( )
{
clrscr ( );
circle c;
clrscr ( );
c.getdata ( );
c. display ( );
getch ( );
}
O/p
Enter radius
3.5
radius = 3.5
area = 38.465
perimeter = 21.98

 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

1. DEFINE A CLASS COMPLEX TO ACCEPT 2 NO’S AND PRINT INFORM OF


RAIONAL AND COMPLEX NUMBERS:-

# 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

PASSING OBJECTS AS PARAMETERS TO THE MEMBERS FUNCTION


1. Addition of 2 complex numbers:-

# 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

=> FUNCTION OVER LOADING


=> Function over loading implement the concept of one name with multiple
forms”
=> We can declare a function for multiple purposes
=> A function can be called by list of parameters given.
Pgm.

# 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

O/p add of 2 integers = 30


Add of 2 double values = 28.9
Add of 2 nos = 70.11

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

2. Pgm to find mean (avg) of 2 given numbers:-

# 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

* A constructor is invoked by itself when object is created.

1. Pgm. Default construction

# 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

2. Addition of 2 complex numbers


# include <iostream.h>
# include <conio.h>
class complex
{
int a,b;
public:
complex ( );
{
a=10, b=20;
}
complex (int x, int y);
{
a = x, b= y;
}
void add (complex c1, complex c2)
{
a = c1.a + c2.a;
b=c1.b + c2.b;
}
void show ( )
{
cout <<”\n\n\t”<< a<<”+i"<<b;
}
};
void main ( )
{
complex c1, c2 (8,6), c3;
clrscr ( );
c1.show ( );
c2. show ( );
c3. add (c1, c2);
c3. show ( );
getch ( );
}
O/p
10+i20 8+i6 18+i26
3. Addition of two rational numbers out side class:-

# 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

-> Defining a Inheritance Class :-


Class alssmamc
{
}
Class derived : visibility mode based class name
{
};
Eg:- Class abc
{
a;
}
Class mno: Public abc
{
};

* Private, Public and protected are known as access specifiers.


-> Protected : By defining data members as protected we can directly
inherit to derived class without any memeberfunction.
Pgms on Single Inheritance
1 ) # include <iostream oh>
# include < ionio.h>
Class first
{ (2) protected
Int a,b;
// Public:
Void inputab( )
{
(out (c” \n\n enter a b values\n”)
(in>>a>>b:
}
Void show ab ( )
{
(out<< “\n\n a = “ << a:
(out<< “\n\n b = “ <<b:
}
};
Class second : Public first:-
{
Int m,n;
Public :
Void input mn ( )
{ (2) input ab ( );
(out <<”\n\n enter mn values \n”;
An >> m>>n;
}
Void show mn( )
{ 2) show ab( );
(out”a; cout cc”\n a=”cc a;
cout cc”\n b=”cc b;
}
};
Void main ( )
{
// Public: ard: ‘fire: : input ab ( ) is not accessible
Second f;
Clrscr ( ) ;
S.input ab ( );
S.input mn ( );
S.show ab ( );
S.show mn ( );
Getch ( ) ;
}
o/p:-
1) method 2) method
Enter a b values enter a b values
10 4
20 6

Enter m n values enter m n values


30 8
40 9=
a = 20 a=4
b = 20 b=6
m = 30 m=8
n = 40 n=9

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

Compile time ploy. Run time ploy.

Function Operator Virtual functions


overloading overloading

 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.

 Pgm on Virtual function .


# include <iostream.h>
# include <conio.h>
Class first
{
Public:
o/p:
Virtual void.show( )
Void virtual show ( )
{
Cout<<”\n\nt class first show”;
}
o/p
Virtual void display ( )
{
Cout ,,”\n\nt class first display”;
}
};
Class second : public first.
{
Public :
Void show ( )
{
Cout <<”\n\n class second show”;
}
Void display ( )
{
Cout <<”\n\n class second display”;
}
};
Void main ( )
{
First f,p; o/p:= class first show
Clrscr ( ); class first display
P = & f; class second show
P -> show ( ); class second display,
P -> display ( );
second s; o/p :- class first show
p=&s class first display
p -> show ( ); class first show
p -> display ( ); class first display
getch ( ) ; o/p Virtual void show ( )
} void display ( ).
Class first show
Class first display
Class second show
Class first display.

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.

24/Oct/08 TEMPLATE WITH MULTIPLE PARAMETER


Friday
1) Pgm :- class template
# include <iostream.h>
# include <ionio.h>
template <classt1,classt2>
class sample
{
t1 a ;
t2 b ;
public :
void set (t1 x, t2 y)
{
a = x , b=y;
}
void show ( )
{
cout <<”\n\nt a = “ <<a;
cout <<”\n\n\t b=”<<b<<”\n\n\n”;
}
};
void main ( )
{
sample (int, float > s1;
clrscr ( );
s1.set (10,15,86);
s1.show ( );
sample <float, char> s2;
s2.set (18.93, ‘M’);
s2.show ( );
sample <char,int> s3;
s3.set (‘S’, 30);
s3.show ( );
getch ( );
}
o/p:-
a = 10
b = 15.86
a = 18.93
b=m
a=s
b = 30

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.

Swapping of two numbers by using template


# include <iostream.h>
# include <ionio.h>
template <class t >
void show (t &x, t &y)
{
cout <<”\n\nt a = “<<x;
cout <<”\n\nt b = “<< y << “\n\n”;
}
template < class t >
void swap (t &x, t &y)
{
t z;
t = x;
x = y;
y = z:
void main ( )
{
int a =10, b =20;
clrscr ( ) ;
show (a,b);
swap ( a,b);
swap (a,b);
show (a,b);
float m = 5.63, n=17.92;
show (m,n);
swap (m,n);
show (m,n);
char p=’A’ , q = ‘B’,
show (p,q);
swap (p,q);
show (p,q);
getch ( );
o/p:-
a = 10
b = 20
a = 20
b =10
a =15.63
b = 17.92
a=A
b=B
a=B
a = A.

You might also like