0% found this document useful (0 votes)
6 views

C++ Practical

Uploaded by

bhavaya231
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

C++ Practical

Uploaded by

bhavaya231
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

INDEX

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

12. Write a program to illustrate Virtual Base Class. 36-39

13. Write a program to illustrate Nesting of Classes. 40-42


14. Write a program to illustrate Inheritance and 43-45
Constructors.
15. Write a program to illustrate Inheritance & 46-48
Destructors.
16. Write a program to illustrate Operator 49-51
Overloading.

1
PRACTICAL NO. 1

➢ Write a Program to Perform Addition of Two


Numbers.

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

➢ CREATE A CLASS TO FIND THE GREATEST NUMBER


AMONG THREE NUMBERS.

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

➢ WRITE A PROGRAM TO FEED STUDENTS


PARTICULAR SUCH AS ROLL NO.,NAME AND
MARKS FROM THE KEYBOARD AND FIND THE
AVERAGE MARKS OF THE STUDENT.

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

➢ WRITE A PROGRAM USING SWAP FUNCTION TO


EXCHANGE TWO OBJECTS.

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

➢ WRITE A PROGRAM USING FRIEND FUNCTION FOR


ADDITION OF TWO NUMBERS.

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

➢ WRITE A PROGRAM TO ILLUSTRATE THE USE OF


LOCAL CLASSES.

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

➢ WRITE A PROGRAM TO ILLUSTRATE THE COPY


CONSTRUCTOR, DEFAULT CONSTRUCTOR,
PARAMITERIZED CONSTRUCTOR.

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

➢ WRITE A PROGRAM TO ILLUSTRATE THE CONCEPT


OF DESTRUCTOR.

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

➢ WRITE A PROGRAM TO ILLUSTRATE MULTIPLE


INHERITANCE.

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

class Derived: public Base, public Base2


{
public:
void display()
{
cout<<\n In Derived Class”;
}
};
main()
{
Derived d;
d.show();
d.getdata();
d.display();
getch();
}

26
OUTPUT

27
PRACTICAL NO. 10

➢ WRITE A PROGRAM TO ILLUSTRATE


HIERARCHICAL INHERITANCE.

INPUT

#include<iostream.h>
#include<conio.h>
class Parent
{
public:
void show()
{
cout<<” \n In Base Class”;
}
};

class Child1: public Parent


{
public:
void output()
{
cout<<” \n In Derived Class”;
}

28
};

class Child2: public Parent


{
public:
void get()
{
cout<<” \n In Derived Class 2”;
}
};
main()
{
Child1 c1;
c1.show();
c1.output();
Child2 c2;
c2.show();
c2.get();
getch();
}

29
OUTPUT

30
PRACTICAL NO. 11

➢ WRITE A PROGRAM TO ILLUSTRATE HYBRID


INHERITANCE.

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

➢ WRITE A PROGRAM TO ILLUSTRATE VIRTUAL BASE


CLASS.

INPUT

#include<iostream.h>
#include<conio.h>
class A
{
public:
void show()
{
cout<<”\n In Class A”;
}
};

class B:virtual public A


{
public:
void get()
{
cout<<” \n In Class B”;
}

36
};

class C: virtual public A


{
public:
void output()
{
cout<<” \n In Class C”;
}
};

class D: public B, public C


{
public:
void display()
{
cout<<” \n In class D”;
}
};
main()
{
D d1;
clrscr();
d1.show();
d1.get();
d1.output();

37
d1.display();
getch();
}

38
OUTPUT

39
PRACTICAL NO. 13

➢ WRITE A PROGRAM TO ILLUSTRATE NESTING OF


CLASSES.

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

➢ WRITE A PROGRAM TO ILLUSTRATE INHERITANCE


AND CONSTRUCTOR.

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

➢ WRITE A PROGRAM TO ILLUSTRATE INHERITANCE AND


DESTRUCTOR.

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

class Derived: public Base


{

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

➢ WRITE A PROGRAM TO ILLUSTRATE OPERATOR


OVERLOADING.

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

You might also like