C++ (OOPs)
C++ (OOPs)
In 1976, Bjarne Stroustrup developed C++ (oops) in BELL laboratories, New Jersey, USA.
First name it as C with classes after that it change to C++ (CPP), because it is increment of C.
Tokens:
Keywords
Characters (Character set)
Constants
Variable
Operators
Key words:
int if continue auto protected
float else default register friend
char while void static virtual
short do return external template
long for struct typedef operator
double switch union class
signed break enum public
unsigned case sizeof private
Data Types: Every variable is a data type that specify the size and type of values that can be
stored.
Built-in type : int, char, float, double, void
User-Defined type : struct, union, class, enum (enumeration)
Derived type : array, function, pointer
Declaration of Variables Names of storage locations
* A variable must be declare before it is used in the program. Variables are separated by
commas.
type v1,v2,v3, .. . . . . vn;
Ex: int number; float per;
char c1,c2; double y;
/* Simple program1 */
#include<iostream.h>
#include<conio.h>
void main( )
{ clrscr( ); Output:-
cout<<”welcome”; Welcome
getch( ); //printf(“welcome”);
}
/* Simple program2 */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x=10;
clrscr( ); Output:-
cout<<”x :”<<x; x:10
getch( ); // cout<<x; 10
}
/* Simple program3 */
#include<iostream.h>
#include<conio.h> Output:-
void main( ) 1.23
{ float x=1.23;
clrscr( ); cout<<x;
getch( );
}
/* Simple program4 */
#include<iostream.h>
#include<conio.h>
void main( )
{ char x=’A’;
clrscr( ); Output:-
cout<<”x:”<<x; x:A
getch( ); //x=a;
}
/* Simple program5 */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x=10,y=3,z;
clrscr( ); z=x+y; Output:-
cout<<x<<”+”<<y<<”=”<<z; 10+3=13
getch( ); // cout<< z; 13
} // printf(“%d+%d=%d”,x,y,z);
/* Simple program read integer */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x; clrscr( ); Output:-
cout<<”enter no :”; enter no :10
cin>>x; // scanf(“%d”,&x); x: 10
cout<<”x :”<<x; getch( );
}
/* Write a program to add two integers */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y,z; clrscr( ); Output:-
cout<<”enter two nos :”; cin>>x>>y; enter two nos :10 5
z=x+y; cout<<z; getch( ); 15
}
/* Write a program to find area of the circle */
#include<iostream.h>
#include<conio.h>
void main( )
{ int r; clrscr( ); Output:-
cout<<”enter r :”; cin>>r; enter r:1
float a=3.14*r*r; cout<<a; 3.14
getch( );
}
/* Write a program to swap two numbers */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y; clrscr( ); Output:-
cout<<”enter two nos :”; cin>>x>>y; enter two nos :10 50
cout<<”x:”<<x<<”y:”<<y<<endl; x:10 y:50
x=x+y; x:50 y:10
y=x-y;
x=x-y;
cout<<”x:”<<x<<”y:”<<y<<endl; getch( );
}
Setw: (Manipulators)
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
void main( )
{ int x=1,y=12,z=123,m=1234,n=12345; Output:-
clrscr( ); 12345
cout<<setw(6)<<n<<endl; 1234
cout<<setw(6)<<m<<endl; 123
cout<<setw(6)<<z<<”\n”; 12
cout<<setw(6)<<y<<endl; 1
cout<<setw(6)<<x<<endl; 12
getch( ); 1 123
} 12
123
1234
Reference Variables:
data-type & reference-name = variable-name
Ex: -
float x = 10; float &y = x;
/*Example program for reference variable */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x=10; int &y = x; // y is a reference variable of x
cout<<”x :”<<x<<”y :”<<y<<endl; O/P:-
x = x + 10; x: 10 y :10
cout<<”x :”<<x<<”y :”<<y<<endl; x: 20 y :20
y =y-5; x: 15 y :15
cout<<”x :”<<x<<”y :”<<y<<endl;
getch( );
}
/* Write a program to add, sub, multi, divide & find remainder of the two nos. */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x,y,a,b,c,e; float d; Output:
clrscr( ); x y enter two nos: 10 3
cout<<“enter two nos:”; cin>>x>>y; a : 13 b : 7 c : 30
a = x + y; b = x – y; 10 3 d : 3.333333 e : 1
c = x * y; a b c d e
d = x / (float) y; //d=x/y;
e = x % y;
cout<<“a :”<<a<<”b :”<<b<<”c :”<<c<<endl;
cout<<“d :”<<d<<”e :”<<e;
getch( );
}
cout<<a<<” ”<<b 13 7 30
LOOPS
Def: -
One or more that one statement(s) are repeated or executed through a condition.
Branching When a program breaks the sequential flow and jumps to another
part of the code, it is called branching
Types of statements: -
I. Conditional statements
1. if 2. switch
II. Iterative statements
1. While 2. do-while 3. for
III. Un-conditional statement
1. goto
If: -
Def: -
Check the condition, when condition is true execute if following statement(s).
Syntax: -
if(cond) if(cond)
statement1; (or) {
B.S.
}
/* Write a program to check the given no is positive. (using if) */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x; Output:
clrscr( ); x enter any no : 10
cout<<“enter any no :”; cin>>x; 10 10 is +ve
if(x>0)
cout<<x<<”is +ve”;
getch( );
}
/* Write a program to check the given no is even. (using if) */
#include<iostream.h>
#include<conio.h>
void main( )
{ int x; x Output:
clrscr( ); 20 enter any no : 20
cout<<“enter any no :”; cin>>x; 20 is even
if(x% 2 = = 0)
cout<<x<<“is even”;
getch( );
}
If-else: -
Def: -
Check the condition, when condition is true execute if following statement(s). When
condition is false else following statement(s) will be executed.
Syntax: -
if(cond) if(cond)
statement1; (or) {
else
statement2; B.S.
}
else
{
B.S.
}
/* Write a program to check the given no is positive or negative. (using if-else) */
#include<iostream.h>
#include<conio.h>
void main( ) x
{ int x; clrscr( ); 12 Output:
cout<<“enter any no :”; cin>>x; enter any no : 12
if(x>0) 12 is +ve
cout<<x<<”is +ve”;
else enter any no : -2
cout<<x<<”is -ve”; -2 is -ve
getch( );
}
/* Write a program to check the given no is even or odd. (using if-else) */
#include<iostream.h>
#include<conio.h>
void main( ) x
{ int x; 10 Output:
clrscr( );
cout<<“enter any no :”; cin>>x; enter any no : 10
if(x% 2 = = 0) 10 is even
cout<<x<< “ is even”; enter any no : 15
else 15 is odd
cout<<x<< “ is odd”;
getch( );
}
else-if ladder: -
If-else is following with another if-else.
Syntax: -
if(cond)
statement1;
else if(cond)
statement2;
else if(cond)
statement3;
else if(cond)
:
Nested if: -
If-else is following with another if-else.
Syntax: -
if(cond)
{
if(cond)
statement1;
}
/* Write a program to find total, percentage & grade of the student when three subjects are given.*/
#include<iostream.h>
#include<conio.h>
void main( )
{ int m1,m2,m3,tot; float per; Output:
clrscr( ); enter three sub marks: 80 80 80
cout<<“enter three sub marks :”; cin>>m1>>m2>>m3; m1 : 80
tot = m1 + m2 + m3; per = tot / 3.0; m2 : 80
cout<< “m1 :”<<m1<<” m2 :”<<m2<<” m3 :”<<m3<<endl; m3 : 80
cout<<“ tot :”<<tot<<” per :”<<per<<endl; tot : 240
if( m1 >= 40 && m2 >= 40 && m3 >= 40 ) per : 80.000000
{ if( per >= 75) distinction
cout<<“distinction”; m1 m2 m3
else if( per >= 60) 80 80 80
cout<<“1st class”;
else if( per >= 50) tot per
cout<<“2nd class”;
else
cout<<“3rd class”;
}
else
cout<<“fail”;
getch( );
}
While: -
Def: -
Check the condition, when condition is true execute the loop until the condition is false.
Syntax: -
while(cond) while(cond)
st1; (or) {
B.S.
}
/* Write a program to print 1,2,3,……… 10 */
#include<iostream.h> Output:
#include<conio.h> 1
void main( ) 2
{ int i=1; clrscr( ); 3
while(i<=10) i :
{ cout<<i<<endl; i=i+1; 1 10
} cout<<i++<<endl;
getch( );
}
}
Exp1: - (Initialization)
Ex: - i=0; j=10; k=1; …….. etc.
Exp2: - (Condition)
Ex: - i<=10; j>=1; k<=100; …….. etc.
Exp3: - (Increment / Decrement)
Ex: - i++; j--; k++; …….. etc.
/* Write a program to print 1, 2, 3, ……… 10 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int i; i 1
clrscr( ); 2
1
for(i=1;i<=10;i++) :
cout<<i<<endl; 10
getch( );
}
/* Write a program to print 2, 4, 6, ……… 10 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int i; clrscr( ); i 2
for(i=2;i<=10;i=i+2) 4
2
cout<<i<<endl; :
getch( ); 10
}
/* Write a program to print 1+ 2 + 3+ ………+10 */
#include<iostream.h>
#include<conio.h> Output:
void main( ) i sum 55
{ int i,sum=0; 1 0
clrscr( );
for(i=1;i<=10;i++)
sum = sum + i;
cout<<sum; getch( );
}
do-while: -
Def: -Execute the loop, after execution of loop check the condition when condition is true execute
the loop until the condition is false.
syntax: -
do
{
B.S.
}while(cond);
switch: -
Select one statement from number of statements.
Syntax: -
switch (op)
{
case ‘char’ (or) const : statement 1; break;
case ‘char’ (or) const : statement 2; break;
case ‘char’ (or) const : statement 3; break;
:
default : statement n;
}
/* Simple program for switch. */
#include<iostream.h>
#include<conio.h>
void main( )
{ int n; clrscr( ); n
cout<<“enter no :”; cin>>n; 3 Output:
switch(n) enter no : 3
{ case 1 : cout<<“ one ”; break; three
case 2 : cout<<“ two ”; break;
case 3 : cout<<“ three ”; break;
case 4 : cout<<“ four ”; break;
default : cout<<“ wrong option ”;
}
getch( );
}
/* Write a program for add, subtract, multiple, divide and find remainder of the two numbers when
operator is given. */
#include<iostream.h> OUTPUT:
#include<conio.h> enter character [+ - * / %]: *
void main( ) enter two nos : 10 3
{ char op; 30
int x,y,a,b,c,e; float d;
clrscr( );
cout<<“enter character [+ - * / %]:”; cin>>op;
cout<<“enter two nos :”; cin>>x>>y;
switch(op)
{ case ’+’ : a = x+y; cout<<a; break;
case ’-’ : b = x-y; cout<<b; break;
case ’*’ : c = x*y; cout<<c; break;
case ’/’ : d = x/(float) y; cout<<d break;
case ’%’ : e= x%y; cout<<e; break;
default : cout<<“invalid option”;
}
getch( );
}
/* Write a program to print corresponding day for the given number (1 for Sunday) */
#include<iostream.h>
#include<conio.h>
enum day
{ sun, mon, tue, wed, thu, fri, sat
};
void main( )
{ int n;
clrscr( );
cout<<"enter n :"; cin>>n; output: -
switch(n) enter n : 1
{ case sun : cout<<"sun"; break; mon
case mon : cout<<"mon"; break;
case tue : cout<<"tue"; break;
case wed : cout<<"wed"; break;
case thu : cout<<"thu"; break;
case fri : cout<<"fri"; break;
case sat : cout<<"sat"; break;
default : cout<<"wrong option";
}
getch( );
}
ARRAYS
Def: -
Collection of similar or related data elements.
Declaration: -
i). Data type Variable[size]={values}; ii). Data type Variable[size];
int x[5]={10,20,30,40,50}; int x[3];
0 1 2 3 4 0 1 2
10 20 30 40 50
float a[3] = {1.1, 1.2, 1.3}; float a[100] ;
0 1 2 0 1 2 3 99
1.1 1.2 1.3
char s[10]={‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’}; char b[5];
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4
a b c d e f g h i j
2D Array: -
Declaration: -
Data Type variable[size][size];
Ex: - int x[60][8];
float a[4][2];
char std[100][20];
6. Write a program to print row and column sum of the given matrix. (3*3)
void main( )
{ int x[3][3],rsum,csum,i,j,tsum=0;
clrscr( ); Output:
cout<<“ enter 9 eles in x : ”; enter 9 eles in x : 1 2 3 4 5 6 7 8 9
for(i=0;i<=2;i++) 1 2 3 =6
for(j=0;j<=2;j++) 4 5 6 =15
cin>>x[i][j]; 7 8 9 =24 0 1 2
12 15 18 =90 0 00 01 02
for(i=0;i<=2;i++) 1 10 11 12
{ rsum=0; 2 20 21 22
for(j=0;j<=2;j++)
{ cout<<x[i][j]<<”\t”;
rsum=rsum+x[i][j];
}
cout<<”=”<<rsum<<”\n”;
tsum=tsum+rsum;
}
for(i=0;i<=2;i++)
{ csum=0;
for(j=0;j<=2;j++)
{ csum=csum+x[j][i];
}
cout<<csum<<”\t”;
tsum=tsum+csum;
}
cout<<”=”<<tsum;
getch( );
}
FUNCTIONS
Function is sub program. It having own name and block of statements. When it is called from main it
will be executed.
main
function
function
function
Types of functions: -
6. Write a program to swapping or interchanging of two numbers. (Using passing by argument &
non-return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void swap( int x, int y ); enter two nos : 10 20
int a,b;
clrscr( ); a : 10 b : 20
cout<<“enter two nos :”; a : 20 b : 10
cin>>a>>b;
cout<<“ a :”<<a<<” b :”<<b<<endl; a b
swap( a,b); 10 20
getch( );
}
void swap( int x, int y )
{ x=x+y; x y
y=x-y; 10 20
x=x-y;
cout<<“ a :”<<x<<” b :”<<y<<endl;
}
7. Write a program to print reverse of the given number. (Using passing by argument &
non-return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ void reverse( int num ); enter no : 123
int n; reverse of the given no :321
clrscr( );
cout<<“enter no :”; cin>>n;
reverse( n ); n
getch( ); 123
}
void reverse( int num )
{ int rem,sum=0; num rem sum
while(num>0) 123 0
{ rem = num % 10;
sum = (sum * 10) + rem;
num = num / 10;
}
cout<<“reverse of the given no :”<<sum;
}
8. Write a program to print reverse of the given number. (Using passing by argument & return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ int reverse( int num ); enter no : 123
int n,t; reverse of the given no :321
clrscr( );
cout<<“enter no :”; cin>>n; n
t=reverse( n ); 123
cout<<“reverse of the given no :”<<t;
getch( );
}
int reverse( int num )
{ int rem,sum=0; num rem sum
while(num>0) 123 0
{ rem = num % 10; 0 321
sum = (sum * 10) + rem;
num = num / 10;
}
return(sum);
}
9. Write a program to print area of the circle. (Using passing by argument & return type)
#include<iostream.h>
#include<conio.h>
void main( ) Output :
{ float area( int r ); enter r : 1
int r; float a; area of the circle : 3.140000
clrscr( ); r a
cout<<“enter r :”; cin>>r;
1
a = area( r );
cout<<“area of the circle :”<<a;
getch( );
}
float area( int r ) r a
{ float a= 3.14 * r * r; 1
return( a );
}
10. Write a program add, subtract, multiply, divide and find reminder of two numbers when
operator is given ( + - * / %).
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ void add(int x, int y); void sub(int x, int y); enter op [+ - * / %]: +
void mul(int x, int y); void div(int x, int y); ente two nos: 10 3
void rem(int x, int y); 13
int a,b; char op; clrscr( );
cout<<“enter op [+ - * / %] :”; cin>>op;
cout<<“enter two nos:”; cin>>a>>b;
switch( op )
{ case ‘+’ : add(a,b); break;
case ‘-’ : sub(a,b); break;
case ‘*’ : mul(a,b); break;
case ‘/’ : div(a,b); break;
case ‘%’ : rem(a,b); break;
default : cout<<“ enter correct option”;
}
getch( );
}
void add(int x, int y)
{ int z; z = x + y; cout<<z; }
void sub(int x, int y)
{ int z; z = x - y; cout<<z; }
void mul(int x, int y)
{ int z; z = x * y; cout<<z; }
void div(int x, int y)
{ float z; z = x /(float) y; cout<<z; }
void rem(int x, int y)
{ int z; z = x % y; cout<<z; }
inline function:
Write a program to add and multiple two numbers using inline function.
#include<iostream.h>
#include<conio.h>
inline int add(int x, int y) Output:
{
int z; 30
z=x+y; 20
return z;
}
inline int mul(int x, int y)
{
int z=x*y;
return z;
}
void main( )
{
clrscr( ); //x=add(10,2); y=mul(12,2);
cout<<add(10,20)<<endl; //cout<<x;
cout<<mul(10,2)<<endl;
getch( );
}
Reference Variable
Function overload:
Def: Name of the functions are same or equal but perform different task.
1. Write a program to swap two numbers and two characters using function overload.
#include<iostream.h>
#include<conio.h>
void swap( int x, int y )
{ x=x+y; x y
y=x-y; x=x-y; 10 20
cout<<x<<” ”<<y<<endl;
}
void swap( char x, char y )
{ char t; x a y b
t=x;
x=y; y=t;
cout<<x<<” ”<<y<<endl; t
}
2. Write a program to area of the circle and area of the rectangle using function overload.
#include<iostream.h>
#include<conio.h>
void area(int r)
{ float a=3.14*r*r;
cout<<a<<endl;
}
void area(int l,int b) Output:
{ int a=l*b; 3.14
cout<<a<<endl; 200
}
void main( )
{ clrscr( );
area(1); area(10,20);
getch( );
}
STRINGS
Declaration : -
i) D.T. v[size]=”string”; ii) D.T. v[size];
ex:- ex:-
char x[5]=”gopi”; char x[10];
0 1 2 3 4 0 1 2 3 4 5 6 7 8 9
g o p i ‘\0’
char y[10]=”abc”; char a[20];
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ----- 19
a b c ‘\0’
String functions: -
1. strlen( )
2. strcpy( )
3. strcmp( )
4. strcat( ) <string.h>
4. Write a program to copy string one variable to other. [Using string function strcpy( )]
#include<iostream.h>
#include<conio.h>
#include<string.h> 0 1 2 3 4 5 6 7 8 9 10 11 12
void main( ) x
g o p i ‘\0’
{ char x[20],y[20];
clrscr( ); 0 1 2 3 4 5 6 7 8 9 10 11 12
cout<< “enter string :”; cin>>x; y
strcpy(y,x);
cout<<“x :”<<x<<” y:”<<y; Output:
getch( ); enter string : gopi
} x : gopi y : gopi
strrev(x); x=dcba
strupr(x);
strlwr(x);
CLASS
Def:
A class is a way of binding the data and associated member functions in a single unit.
Object:
Instance of a class is known as object (Instance is a mechanism of allocating sufficient
amount of memory space for data members of a class)
Class variable is known as an object (or) blue print of a class is known as an object or value
of a class is known as a object or real world entities of a class.
Data Abstraction:
It is a mechanism of retrieving the essential details without dealing with background details.
Encapsulation:
It is the process of wrapping up on data and associated methods in a single unit. It is basically
used for achieving data/information hiding i.e. security.
Declaration:
class class_name
{ protected:
instance variables
member functions
private:
instance variables
member functions
public:
instance variables
member functions
};
class_name obj_list;
void main( )
{ student s;
clrscr( );
s.read( ); s.display( );
getch( );
}
2. Write a program to print book details using class.
#include<iostream.h>
#include<conio.h>
class book name author price
{ private:
char name[20]; b b
char author[20];
float price;
public: Output:
void read( ) enter name : c
{ cout<<“enter name :”; cin>>name; enter author : swamy
cout<<“enter author :”; cin>>author; enter price : 300
cout<<“enter price :”; cin>>price; name : 1
} author : gopi
void display( ) price : 300.00
{ cout<<“name :”<<name<<endl;
cout<<“author :”<<author<<endl;
cout<<“price :”<<price<<endl;
}
};
void main( )
{ book b; clrscr( );
b.read( ); b.display( );
getch( );
}
void main( )
{ employee e;
clrscr( );
e.getdetails( ); e.display( );
getch( );
}
4. Write a program to print super market details using class.
#include<iostream.h>
#include<conio.h>
class market item qty price totamt
{ char item[20];
int qty; float price,totamt; m
public: Output:
void getitem( ) enter item : pen
{ enter qty : 5
cout<<“enter item :”; cin>>item; enter price : 10
cout<<“enter qty :”; cin>>qty;
cout<<“enter price :”; cin>>price; item : pen
} qty :5
void display( ); price : 10
}; totamt: 50
void market :: display( )
{ totamt=qty*price;
cout<<“item :”<<item<<endl;
cout<<“qty :”<<qty<<endl;
cout<<“price :”<<price<<endl;
cout<<“totamt:”<<totamt;
}
void main( )
{ market m;
clrscr( );
m.getitem( ); m.display( );
getch( );
}
5. Write a program to print student details using class and passing by argument.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{ int rno; char name[20]; rno name per
float per;
public: s
void read(int r,char n[ ],float p)
{ rno=r; strcpy(name,n); Output:
per=p; enter rno : 1
} enter name : gopi
void display( ) enter per : 70
{ cout<<“rno :”<<rno<<endl; rno :1
cout<<“name :”<<name<<endl; name : gopi
cout<<“per :”<<per<<endl; per : 70.00
}
};
void main( )
{ int r; char n[20]; float p;
student s;
clrscr( );
cout<<“enter rno :”; cin>>r;
cout<<“enter name :”; cin>>n;
cout<<“enter per :”; cin>>p;
s.read(r,n,p); s.display( );
getch( );
}
int add(addition a)
{ return(a.x+a.y);
}
void main( )
{ addition a;
int p,q,k;
clrscr( );
cout<<"enter two numbers :"; cin>>p>>q;
a.write(p,q);
k=add(a);
cout<<k;
getch( );
}
Array of objects
#include<iostream.h>
#include<conio.h>
class employee Output:
{ int id; char *name; float sal;
public: enter emp id :
void getdata( ); void print( ); enter emp name:
}; enter emp sal:
void employee :: getdata( ) :
{ cout<<"enter emp id :"; cin>>id; :
cout<<"enter emp name :"; cin>>name;
cout<<"enter emp sal :"; cin>>sal; emp id :
} emp name :
void employee :: print( ) emp sal :
{ cout<<"emp id :"<<id<<endl;
cout<<"emp name :"<<name<<endl;
cout<<"emp sal :"<<sal<<endl;; id name sal
}
const int size = 3; 0 101 aaa 10000
void main( ) 1 102 bbb 12000
{ employee e[size]; int i; 2 103 ccc 15000
clrscr( );
for(i=0;i<=2;i++)
e[i].getdata( );
for(i=0;i<=2;i++)
e[i].print( );
getch( );
}
Constructers and Destructors
Def: Name of the class and its function(s) names are equal. When object is created automatically
function will be invoke (called).
Types of Constructors:
i) Default Constructors ii) Parameterized Constructors
Default Arguments
/* Multiply two numbers using Default Arguments constructors. */
#include<iostream.h>
#include<conio.h>
class multiply
{ int x,y;
public: Output:
multiply(int a=0,int b=0)
{ x=a; y=b; 14
} 0
int display( )
{ return (x*y);
}
};
void main( )
{ clrscr( );
multiply m(2,7); cout<<m.display( )<<endl;
multiply n; cout<<n.display( )<<endl;
getch( );
}
/* Area of the circle using constructor */
#include<iostream.h>
#include<conio.h>
class area
{ float r,a;
public:
area( )
{ r=1; }
area(float r1); // area(float r1)
void cal( ) // {
{ a=3.14*r*r; cout<<a<<"\n"; // r=r1;
} // }
};
area :: area(float r1) Output:
{ r = r1;
} 12.56
void main( ) 3.14
{ clrscr( );
area ac1(2); ac1.cal( );
area ac2; ac2.cal( );
getch( );
}
This:
#include<iostream.h>
#include<conio.h>
class area
{ float r,a;
public:
area( )
{ r=1;
}
area(float r);
void cal( )
{
a=3.14*r*r; cout<<a<<"\n";
}
};
area :: area(float r)
{
this->r = r; // r= r;
}
void main( ) Output:
{ clrscr( );
area a(2); a.cal( ); 12.56
getch( );
}
delete:
delete pointer-variable;
Ex:- delete p;
delete q;
delete [size]pointer-variable;
Ex:- delete [ ]p;
delete [ ]q;
/* Sample program1 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int *p;
clrscr( ); 100
p = new int; *p = 100;
cout<<"*p :"<<*p;
delete p;
getch( );
}
/* Sample program2 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int *p;
clrscr( ); 10
p = new int(10); cout<<"*p :"<<*p;
delete p;
getch( );
}
Allocating Arrays
var = new arr_type[size];
delete [ ]var;
Ex:-
x = new int[10];
delete [ ]x;
/* Sample program3 */
#include<iostream.h>
#include<conio.h>
void main( ) Output:
{ int *p,i;
clrscr( ); 1
p = new int[10]; 2
for(i=0;i<10;i++) :
p[i] = i+1; :
for(i=0;i<10;i++) 10
cout<<p[i]<<endl;
delete [ ]p; p 0 1 2 9
getch( ); 1 2 3 10
}
Operator Overloading
/* Write a program to display negation of the given numbers using operator overloading */
#include<iostream.h>
#include<conio.h> Output:
class negitive
{ int x,y,z; x : 10 y : 20 z : 30
public: x : -10 y : -20 z : -30
void getdata(int a,int b,int c);
void display( );
void operator -( );
};
void negitive :: getdata(int a,int b,int c)
{ x = a; y = b; z = c;
}
void negitive :: display( )
{ cout<<" x : "<<x<<" y :"<<y<<" z :"<<z<<endl;
}
void negitive :: operator -( )
{ x = -x; y = -y; z = -z;
}
void main( )
{ negitive n;
clrscr( );
n.getdata(10,20,30); n.display( );
-n; n.display( );
getch( );
}
/* Write a program to check the given two students marks are equal or not equal using operator
overloading */
#include<iostream.h>
#include<conio.h> Output:
class equal
{ int m1,m2; m1 : 90 m2 : 100
public: m1 : 80 m2 : 90
equal( ) not equal
{ m1=0; m2=0; }
equal(int a,int b);
void display( );
int operator = =(equal s);
};
equal :: equal(int a,int b)
{ m1=a; m2=b;
}
void equal :: display( )
{ cout<<”m1 :”<<m1<<”m2:”<<m2<<endl;
}
int equal :: operator ==(equal s)
{ if( (m1= =s.m1) && (m2 = = s.m2) )
return (1);
else
return(0); // while(1)
} // cout<<”gopi\n”;
void main( )
{ clrscr( );
equal s1(90,100), s2(80,90);
s1.display( ); s2.display( );
if(s1= =s2)
cout<<”equal”;
else
cout<<”not equal”;
getch( );
}
/*Write a program to check the given two strings are equal or not equal using operator overloading */
#include<iostream.h>
#include<string.h>
#include<conio.h> Output:
class equal
{ char name[20]; name : gopi
public: name : krishna
equal( ) not equal
{ strcpy(name,” ”); }
equal(char s[ ])
{ strcpy(name,s);
}
void display( );
int operator = =(equal s);
};
void equal :: display( )
{ cout<<”name :”<<name<<endl;
}
int equal :: operator ==(equal s)
{ if( strcmp(name,s.name) = = 0)
return (1);
else
return(0);
}
void main( )
{ clrscr( );
equal s1(“gopi”), s2(“krishna”);
s1.display( ); s2.display( );
if(s1= =s2)
cout<<”equal”;
else
cout<<”not equal”;
getch( );
}
Def:
The process of acquiring the properties of base class to derived class is known as Inheritance.
Note: Private data members and private methods of the base class cannot be inherited at all.
Types of Inheritance:
i) Single Inheritance ii) Multiple Inheritance
iii) Multilevel Inheritance iv) Hierarchical Inheritance
v) Hybrid Inheritance
A A B
B C
A A
B C D B
C
5. Hybrid Inheritance
B C
Single Inheritance:
The process of deriving the properties of class B from class A [existing class]. In this, we
have only one base class and one derived class.
Syntax:
class Base_classname
{ instance variables;
function definition
{ ----
}
}
class Derived_classname : access-specifier Base_classname
{ instance variables;
function definition
{ ----
}
}
/* Write a program to print student details using single inheritance */
#include<iostream.h>
#include<conio.h>
class student
{ protected :
int rno; char name[20];
public :
void read1( )
{ cout<<"enter rno :"; cin>>rno;
cout<<"enter name :"; cin>>name;
}
void display1( )
{
cout<<"rno :"<<rno<<endl;
cout<<"name :"<<name<<endl;
}
};
class address : public student
{ protected :
char fname[20],hno[20],dist[20];
public :
void read2( )
{ read1( );
cout<<"enter fname :"; cin>>fname;
cout<<"enter hno :"; cin>>hno;
cout<<"enter dist :"; cin>>dist;
}
void display2( )
{ display1( );
cout<<"fname :"<<fname<<endl;
cout<<"hno :"<<hno<<endl;
cout<<"dist :"<<dist<<endl;
}
};
void main( )
{ clrscr( );
address a;
a.read2 ( ); a.display2( ); // a.read1(); a.display1();
getch( );
}
Multiple Inheritance
Hybrid Inheritance
In this inheritance we have only single base and more than one derived class.
void display1( )
{ cout<<"rno :"<<rno<<endl;
cout<<"name :"<<name<<endl;
}
};
class marks : public student
{ protected :
int m1,m2,m3,tot;
public :
void read2(int x,int y,int z)
{ m1 = x; m2 = y; m3=z;
tot = m1 + m2 + m3;
}
void display2( )
{ cout<<"m1 :"<<m1<<endl;
cout<<"m2 :"<<m2<<endl;
cout<<"m3 :"<<m3<<endl;
cout<<"tot :"<<tot<<endl;
}
};
class sports Output: enter rno : 1
{ protected : enter name : gopi
char game[20]; enter game : cricket
int score; enter score : 100
public : rno : 1
void read3( ) name :gopi
{ cout<<"enter game :"; cin>>game; m1 : 90
cout<<"enter score :"; cin>>score; m2 : 90
} m3 : 90
void display3( ) tot : 270
{ cout<<"game :"<<game<<endl; game : cricket
cout<<"score :"<<score<<endl; score : 100
} total : 370
};
Template: Template is a variable, we can store any type of value in that variable in any time.
/* Write a program to send student details into the file and read student details from the file */
#include<fstream.h>
#include<conio.h>
void main( )
{ clrscr( );
ofstream p1(“student”); // write mode Output:
int rno; char name[20]; float per; enter rno:1
cout<<”enter rno:”; cin>>rno; enter name:gopi
cout<<”enter name :”; cin>>name; enter per: 70.5
cout<<”enter per:”; cin>>per;
p1<<rno<<endl; p1<<name<<endl; p1<<per<<endl; rno: 1
p1.close( ); name: gopi
ifstream p2(“student”); // read mode per: 70.5
p2>>rno; p2>>name; p2>>per;
cout<<”rno :”<<rno<<endl;
cout<<”name :”<<name<<endl;
cout<<”per :”<<per;
p2.close( ); // FILE *p;
getch( ); // p=fopen(“student”,”w”); fclose(p);
}
/* Write a program to send five(5) strings into the file and read five(5) strings from the file */
#include<fstream.h>
#include<conio.h>
void main( )
{ clrscr( ); int i; char name[20]; Output:
ofstream p1; // write mode enter name: Hyd
p1.open(“names”); enter name: Nlg
clrscr( ); enter name: Adb
for(i=1;i<=5;i++) enter name: Wrl
{ cout<<”enter name:”; cin>>name; enter name: Sec
p1<<name<<endl;
} name: Hyd
p1.close( ); name: Nlg
ifstream p2; // read mode name: Adb
p2.open(“names”); name: Wrl
for(i=1;i<=5;i++) name: Sec
{ p2.getline(name,20);
cout<<”name :”<<name<<endl;
}
p2.close( );
getch( );
}