C++ Practical
C++ Practical
SIGNATURES
SERIAL
NO.
TABLE OF CONTENTS PAGE
NO.
1. Write a Program to Perform Addition of Two 2-4
Numbers.
2. Create a Class to Find the Greatest no. among 3 5-7
Numbers.
3. Write a program …….from keyboard and find 8-10
the average marks of Students.
4. Write a program using Swap Function to 11-13
exchange 2 objects.
5. Write a program using Friend Function for 14-16
adding two numbers.
6. Write a program to illustrate how to use local 17-18
classes.
7. Write a program to illustrate the copy, default, 19-21
parametrized constructor.
8. Write a program to illustrate the concept of 22-24
Destructor.
9. Write a program to illustrate Multiple 25-27
Inheritance.
10. Write a program to illustrate Hierarchical 28-30
Inheritance.
11. Write a program to illustrate Hybrid Inheritance. 30-35
1
PRACTICAL NO. 1
INPUT
#include<iostream.h>
#include<conio.h>
int Add (int a, int b)
{
int c;
c = a + b;
return c;
}
float Add (float x, float y)
{
float z;
z = x + y;
return z;
}
main ()
{
int i, j, k;
float a, b, c;
2
cout<<” Enter Two Numbers” <<endl;
cin>> i>> j;
k= Add (i, j);
cout<<” The Addition is:” << k<< endl;
cout<<” Enter Two Numbers of Float” <<endl;
cin>>a>> b;
c=Add (a, b);
cout<<” The Addition is:” <<c<< endl;
getch();
}
3
OUTPUT
4
PRACTICAL NO. 2
INPUT
#include<iostream.h>
#include<conio.h>
class GreaterNo
{
int num1, num2, num3;
public:
void input()
{
cout<<” Enter Three Numbers” <<endl;
cin>>num1 >>num2 >>num3;
}
int Compare
{
int max;
max=num1;
if(num2>max)
max=num2;
5
if(num3>max)
max=num3;
return max;
}
};
main()
{
GreaterNo G;
int i;
G.input();
i = G.Compare();
cout<<” The Greatest Number is:” <<i<< endl;
getch();
}
6
OUTPUT
7
PRACTICAL NO. 3
INPUT
#include<iostream.h>
#include<conio.h>
class Student
{
int roll;
char name[10];
public:
int marks;
void getdata()
{
cout<<” Enter the Roll no.”;
cin>>roll;
cout<<” \n Enter the Name”;
cin>>name;
cout<<” \n Enter the marks “;
cin>>marks;
}
8
};
const int size = 3;
void main()
{
Student s[size];
int sum=0, i;
float mean;
clrscr();
for(i=0; i<size; i++)
{
cout<<” \n Enter the details of Student: ”<< i+1<< endl;
s[i].getdata();
sum= sum+ s[i].marks;
}
mean= sum/size;
cout<<”\n Average = “<<mean;
getch();
}
9
OUTPUT
10
PRACTICAL NO. 4
INPUT
#include<iostream.h>
#include<conio.h>
Class Teacher
{
int id;
char name[10];
public:
void setdata()
{
cout<<” Enter id”;
cin>>id;
cout<<” Enter Name”;
cin>>name;
}
void getdata()
{
cout<<” ID = “<<id<<endl;
cout<<”Name = “<<name<<endl;
11
}
};
void Swap(Teacher &t1, Teacher &t2)
{
Teacher temp = t1;
t1=t2;
t2=temp;
}
main()
{
Teacher t1,t2;
clrscr();
cout<<” \n Enter Data for Teacher 1 “<<endl;
t1.setdata();
cout<<”\n Enter Data for Teacher 2”<<endl;
t2.setdata();
cout<<”\n After calling function swap() \n”;
swap(t1,t2);
cout<<”Data for Teacher 1: “<<endl;
t1.getdata();
cout<<”Data for Teacher 2: “<<endl;
t2.getdata();
getch();
}
12
OUTPUT
13
PRACTICAL NO. 5
INPUT
#include<iostream.h>
#include<conio.h>
class Random
{
int i, j;
public:
void set()
{
i=30;
j=20;
}
friend int sum (Random x)
{
return (x.i + x.j);
}
};
main()
{
14
Random a;
clrscr();
a. set();
cout<<” \n The Sum is: “<<sum(a);
getch();
}
15
OUTPUT
16
PRACTICAL NO. 6
INPUT
#include<iostream.h>
#include<conio.h>
void fun()
{
class test
{
public:
static void method()
{
cout<<” Local Class Method() called”;
}
};
test :: method();
}
main()
{
fun();
17
getch();
}
OUTPUT
18
PRACTICAL NO. 7
INPUT
#include<iostream.h>
#include<conio.h>
class Square
{
int side, area;
public:
Square ()
{
side = 0;
}
Square (int i)
{
side=i ;
}
Square (Square &r)
{
side= r.side;
19
}
void calculate()
{
area = side * side;
}
void show()
{
cout<<”\n The Side of Square is “ <<side;
cout<<”\n The Area of Square is “<<area;
}
};
main()
{
Square s1(15);
Square s2(s1);
Square s3(5)
clrscr();
s1.calculate();
s2.calculate();
s3.calculate();
s1.show();
s2.show();
s3.show();
getch();
}
20
OUTPUT
21
PRACTICAL NO. 8
INPUT
#include<iostream.h>
#include<conio.h>
class code
{
int a, b;
public:
code( int i, int j)
void display(void);
code();
}
code :: code(int i, int j)
{
cout<<endl <<a<<”\t”<<b;
cout<<”\n Destructor Called”;
}
code :: code()
{
22
cout<<”\n Destructor Called”;
}
main()
{
code c(10, 15);
clrscr();
c.display();
getch();
}
23
OUTPUT
24
PRACTICAL NO. 9
INPUT
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void show()
{
cout<<”\n In Base Class”;
}
};
class Base2
{
public:
void getdata()
{
cout<<”\n In Base Class 2”;
}
25
};
26
OUTPUT
27
PRACTICAL NO. 10
INPUT
#include<iostream.h>
#include<conio.h>
class Parent
{
public:
void show()
{
cout<<” \n In Base Class”;
}
};
28
};
29
OUTPUT
30
PRACTICAL NO. 11
INPUT
#include<iostream.h>
#include<conio.h>
class student
{
protected:
int rollno;
public:
void getno (int a)
{
rollno=a;
}
void putno()
{
cout<<”\n Roll No.: “<<rollno <<endl;
}
};
class test: public student
31
{
protected:
float part1, part2;
public:
void getmarks()
{
part1 = x;
part2 = y;
}
void putmarks()
{
cout<<”Marks Obtained: “<<endl;
cout<<”Subject 1= “<<part1 <<endl;
cout<<”Subject 2= “part2 << endl;
}
};
class Sports
{
protected:
float score;
public:
void getscore()
{
score=s;
}
void putscore()
32
{
cout<<”Sports wt: “<<score<<endl<<endl;
}
};
class result: public test, public sports
{
float total;
public:
void display();
};
void result :: display()
{
total= part1 + part2 + score;
putno();
putscore();
putmarks();
cout<<” Total Score: “<< total<< endl;
}
main()
{
result r1;
clrscr();
r1.getno(1234);
r1.getmarks(56,76);
r1.getscore(6.0);
r1.display();
33
getch();
}
34
OUTPUT
35
PRACTICAL NO. 12
INPUT
#include<iostream.h>
#include<conio.h>
class A
{
public:
void show()
{
cout<<”\n In Class A”;
}
};
36
};
37
d1.display();
getch();
}
38
OUTPUT
39
PRACTICAL NO. 13
INPUT
#include<iostream.h>
#include<conio.h>
class A
{
public:
class B
{
int num;
public:
void getdata()
{
num=n;
}
void putdata()
{
cout<<”\n The Number is “<<num;
}
40
};
};
main()
{
cout<<”\n Nested classes in C++ “<<endl;
A :: B obj;
obj.getdata();
obj.putdata();
getch();
}
41
OUTPUT
42
PRACTICAL NO. 14
INPUT
#include<iostream.h>
#include<conio.h>
class Base
{
public:
Base()
{
cout<<”\n Default of Base”;
}
Base (int x)
{
cout<<”Parameter of Base “<<x <<endl;
}
};
class Derived: public Base
{
public:
Derived()
43
{
cout<<” Default of Derived” <<endl;
}
Derived (int a)
{
cout<<” Parametrized of Derived” <<a <<endl;
}
};
main()
{
Derived d1;
Derived d (50);
getch();
}
44
OUTPUT
45
PRACTICAL NO. 15
INPUT
#include<iostream.h>
#include<conio.h>
class Base
{
public:
Base()
{
cout<<”\n Constructing the Base Class \n”;
}
virtual ~Base ()
{
cout<<” Destructing the Base Class \n”;
}
};
46
public:
Derived()
{
cout<<” Constructing the Derived class \n”;
}
~Derived()
{
cout<<” Destructing the Derived Class \n”;
}
};
main()
{
Derived *d=new Derived();
Base *b=d;
delete b;
getch();
}
47
OUTPUT
48
PRACTICAL NO. 16
INPUT
#include<iostream.h>
#include<conio.h>
class Complex
{
int I ,r;
public:
void insert()
{
cout<<” \n Enter the Real Part”;
cin>>r;
cout<<”\n Enter the Imaginary Part”;
cin>>I;
}
void display()
{
cout<< endl<< “The Complex no.: “<<r << “+” <<I <<”i” <<endl;
}
49
Complex operator +(Complex c2)
{
Complex c3;
c3.r = r+ c2.r;
c3. I= I+ c2.I;
return c3;
}
void show()
{
cout<<endl <<”The Addition of Complex No. is “<<r <<”+” <<I <<”i”;
}
};
main()
{
Complex c1,c2,c3;
c1.insert();
c1.display();
c2.insert();
c2.display();
c3 = c1 + c2;
c3.show();
getch();
}
50
OUTPUT
51
52