C++ Practical Programs
C++ Practical Programs
LABORATORY MANUAL
Prepared by
J.DEEPA M.SC(IT)., M.Phil.,B.Ed
ASSISTANT PROFESSOR
ALGORITHM:
step 1: start the program
step 2: Declare and define functions
void display(int,double);
void display(int,int)
step 3: Declare and define inline function inline void display(double)
step 4: Main function can be declared and defined
step 5: Call the functions Stop the program.
PROGRAM:
#include <iostream.h>
#include<conio.h>
cout << " \n double number: " << var2 << endl;
void main( )
{
int a=5;
double b = 5.5;
clrscr( );
display(a,8);
display(a);
display(b);
getch( );
OUTPUT:
FUNCTION OVERLOADING
Integer number: 5
Integer number: 8
integer number:5
RESULT:
Thus the program has been completed successfully.
2. class and objects
AIM:
To implement class, object and member functions.
ALGORITHM:
step 1: Start the execution.
step 2: Create the class room and declare member variable and member functions calculateArea( ),calculate
Volume( ).
step 3: Create an object to access the member function.
step 4: calculateArea( ) function is used to return the product calculate of length *breath.
step 5: calculateVolume ( ) function is used to find the return product of length*breath*height.
step 6: Finally display the result of the room.
step 7: Stop the program.
PROGRAM:
#include <iostream.h>
#include<conio.h>
class Room
public:
double length;
double breadth;
double height;
double calculateArea()
double calculateVolume()
};
void main( )
{
Room room1;
clrscr( );
cout<<"\n\t\t\t—~—------------—\n";
cout << "\n\n Area of Room = " << room 1.calculate Area( );
getch();
Output:
USING CLASS AND OBJECT
Result:
Thus the program has been completed successfully.
3. Passing Objects to Function (call by value)
AIM:
To implement passing objects to function(call by value).
ALGORITHM:
step 1: Start the program.
step 2: Create a class sample.
step 3: Declare and define the function set(int),pass(sample object 1,sample object 2),print().
step 4: Create the object as a function argument.
step 5: The value of the object should be changed inside the function.
step 6: Main function can be declared and defined.
step 7: Create the objects S1,S2,S3.
step 8: Assigning values to the data member of object.
step 9: Call the print() and pass().
step 10: Stop the program.
PROGRAM:
#include <iostream.h>
#include<conio.h>
class Sample
{
private:
int num;
public:
void set (int x)
{
num = x;
}
void pass(Sample obj1, Sample obj2) //objects are passed
{
obj1.num=100;
obj2.num=200;
cout<<"\n\n Changed value of object1 "<<obj1.num;
cout<<"\n\n Changed value of object2 "<<obj2.num;
}
void print( )
{
cout<<num;
}
};
void main()
{
Sample s1;
Sample s2;
Sample s3;
s1.set(10);
s2.set(20);
cout<<"\n\t\t Example program for pass by value\n\n\n";
cout<<"\n\nValue of object1 before passing";
s1.print();
cout<<"\n\nValue of object2 before passing ";
s2.print();
s3.pass(s1,s2);
cout<<"\n\nValue of object1 after passing ";
s1.print();
cout<<"\n\nValue of object2 after passing ";
s2.print();
getch();
}
Output:
Example program for PASS BY VALUE
Value of object1 before passing 10
Value of object2 before passing 20
Changed value of object 1 100
Changed value of object 200
Value of object 1 after passing 10
Value of object 2 after passing 20 Result:THUS THE PROGRAM HAS BEEN COMPLETED
SUCCESSFULLY.
4. friend function
AIM: To create a program for implement the concept of friend function.
ALGORITHM:
step 1: Start the program.
step 2: Declare the class name as box with data members and member functions.
step 3: The function box( ) is default constructor to initialize the length.
step 4: Declare the friend function friend int print length(box)inside.
step 5: Outside the class to define the friend function and do the following.
step 6: Print the print length(b) in the class.
step 7: Stop the program.
Program:
#include <iostream.h>
#include<conio.h> class
Box
Private:
Box(): length(0) { }
friend int printLength(Box); //friend function
};
int printLength(Box b)
b.length += 10;
return b.length;
void main()
Box b;
clrscr();
cout<<"\n\t\t\tFRIEND FUNCTION";
cout<<"\n\t\t\t---------------------";
cout<<"\n\nLength of box: "<< printLength(b)<<endl;
getch();
}
Output:
Length of the box:10
RESULT:
Thus the program has been completed successfully.
ALGORITHM:
step 1: Start the program.
step 2: Create a class sample.
step 3: Declare and define the function set(int),pass(sample object 1,sample object 2),print().
step 4: Create the object as a function argument with help of &[reference operator].
step 5: The value of the object should be changed inside the function.
step 6: Main function can be declared and defined.
step 7: Create the objects S1,S2,S3.
step 8: Assigning values to the data member of object.
step 9: Call the print() and pass().
step 10: Stop the program.
Program:
#include <iostream.h>
#include<conio.h>
class Sample
{
private:
int num;
public:
void set (int x)
{
num = x;
}
void pass(Sample &obj1, Sample &obj2)
{
obj1.num=100;
obj2.num=200;
cout<<"\n\n Changed value of object1 "<<obj1.num;
cout<<"\n\n Changed value of object2 "<<obj2.num
}
void print()
{
cout<<num;
}
};
void main()
{
clrscr();
Sample s1;
Sample s2;
Sample s3;
s1.set(10);
s2.set(20);
cout<<"\n\t\t Example program for pass by reference\n\n\n";
cout<<"\n\nValue of object1 before passing"; s1.print();
cout<<"\n\nValue of object2 before passing "; s2.print();
cout<<"\n\nValue of object1 after passing "; s1.print();
cout<<"\n\nValue of object2 after passing ";
s2.print();
getch();
}
Output:
RESULT:
Thus the program has been completed successfully.
ALGORITHM:
step 1: Start the program
step 2: Declare the class or cube with side variable.declare the constructor function and it will be initiated.
step 3: Define the constructor inside the class definition.
step 4: Define the destructor inside the class.
step 5: Main function can be declared and defined.
step 6: Create a object c,c1(20),c2.
step 7: Call the constructor functions.
step 8: Call the destructor functions
step 9: Stop the program.
PROGRAM:
#include<iostream.h>
side = 10;
Cube(int x)
side=x;
side = c.side;
~Cube()
cout<<"\n\t\tDestructor is called";
}
};
void main()
{
Cube c,c1(20),c2; clrscr();
cout<<"\n\t\t\tCONSTRUCTOR and DESTRUCTOR";
cout<<"\n\t\t\t "; cout<<"\n\tDefault
Constructor : "<<c.side; cout<<"\
n\tParameterized Constructor : "<<c1.side; c2=c;
cout<<"\n\tCopy Constructor: "<<c2.side; cout<<"\n\
tDestructor:\n\t\t";
getch();
}
Output:
Constructor and destructor
Destructor :
Destructor is called
Destructor is called
Destructor is called
RESULT:
Thus the program has been completed successfully.
Program:
#include <iostream.h>
#include<conio.h>
class Distance
{
private:
int feet;
int inches;
public:
Distance()
{
feet = 0;
inches = 0;
}
Distance(int f, int i)
{
feet = f;
inches = i;
}
displayDistance()
{
cout << "F: " << feet<<"\t"<< " I:" << inches <<endl;
}
Distance operator- ()
{
feet = -feet;
inches = -inches;
return Distance(feet, inches);
}
Distance operator++()
{
++feet;
++inches;
return Distance(feet,inches);
}
}
void main()
{
Distance D1(11, -10);
clrscr();
cout<<"\n\t\t\t\tUNARY OPERATOR";
cout<<"\n\t\t\t\t-----------------\n\n\n";
cout<<"Before using Unary Operator the Value is :\n";
D1.displayDistance();
cout<<"\n\tUsing Unary Operator (-)Negative\n";
-D1;
D1.displayDistance();
cout<<"\n\tUsing unary Operator (++) Incremental \n";
++D1;
D1.displayDistance();
getch();
}
Output:
Unary operator
Before using unary operator the value is: F:11 1:-10
Using unary operator(-)negative F:-11 1:10
Using unary operator(++)incremental F:-10 1:11
RESULT:
Thus the program has been completed successfully.
ALGORITHM:
step 1: Start the program.
step 2: Declare the class distance.
step 3: Declare the variable feet&inch.
step 4: Declare and define the function distance() and distance(int,int)for binary operator overloading.
step 5: Define the friend for binary operator outside the class.
step 6: Main function can be declared and defined.
step 7: Create the object d1,d2,d3 and pass the value to the data members.
step 8: Use the overloaded operator d3=d1+d2.
step 9: Display the result.
Program:
#include <iostream.h>
#include<conio.h>
class Distance {
public:
feet, inch;
Distance()
{
this->feet = 0;
this->inch = 0;
}
Distance(int f, int i)
{
this->feet = f;
this->inch = i;
}
BINARY OPERATOR
Feet :8 inches :9
RESULT:
ALGORITHM:
PROGRAM:
#include <iostream.h>
#include<conio.h>
class Base
{
public:
int base_value;
void base_input()
{
cout<<"Enter the integer value of base class: ";
cin>>base_value;
}
};
class Derived : public Base
{
int derived_value;
public:
void derived_input()
{
cout<<"Enter the integer value of derived class: ";
cin>>derived_value;
}
void sum()
{
cout << "The sum of the two integer values is: " << base_value +
derived_value<<endl;
}
};
void main()
{
Derived d;
clrscr();
cout<<"\n\t\t\tSINGLEINHERITANCE";
cout<<"\n\t\t\t-------------------\n";
d.base_input();
d.derived_input(d.sum();
getch();
}
Output:
Single inheritance
ALGORITHM:
step 1: Start the program.
step 2: Create the base class shape.
step 3: Declare the member function and data member.
step 4: Create another base class paint cost.
step 5: Declare the member function and data member.
step 6: Create a derived class rectangle that is inherited from two base class such as shape and paint
cost.
step 7: Declare the member function and data member.
step 8: Main function can be declared and defined.
step 9: Create the object rect.
step 10: Call the functions with help of the object.
step 11: Display the result.
Program:
#include <iostream.h>
#include<conio.h>
class Shape
{
public:
void setWidth(int w)
{
width = w;
cout<<"\n Width : "<<width;
}
void setHeight(int h)
{
height = h;
cout<<"\n Height : "<<height;
}
protected:
int width;
int height;
};
class PaintCost
{
public:
int getCost(int area)
{
return area * 70;
}
};
void main(void)
{
Rectangle Rect;
int area;
clrscr();
cout<<"\n\t\t\tMULTIPLE INHERITANCE";
cout<<"\n\t\t\t ";
Rect.setWidth(5);
Rect.setHeight(7);
area = Rect.getArea();
cout << "\n\nTotal area: " << Rect.getArea() << endl;
cout << "Total paint cost: $" << Rect.getCost(area) << endl;
getch();
}
Output:
Multiple inheritance
Width:5
Height:7
Total area:35
RESULT:
Thus the program has been completed successfully.
9(c)multilevel inheritance
AIM: To create a program for implementing the concept of multilevel inheritance.
ALGORITHM:
step 1: Start the program.
step 2: Create a base class base.
step 3: Declare the member function and data member.
step 4: Create a derived class derived 1 that is inherited from the base class base.
step 5: Declare the member function and data member.
step 6: Create a derived class derived 2 that is inherited from the base class derived 1.
step 7: Declare the member function and data member.
step 8: Main function can be declared and defined.
step 9: Create the object d2.
step 10: Call the function with help of the object.
step 11: Display the result.
Program:
#include <iostream.h>
#include<conio.h> class
Base
{
public:
int base_value;
void Base_input()
{
cout<<"Enter the integer value of base class: ";
cin>>base_value;
}
};
class Derived1 : public Base // Derived class of base class
{
public:
int derived1_value;
void Derived1_input()
{
cout<<"Enter the integer value of first derived class: ";
cin>>derived1_value;
}
};
class Derived2 : public Derived1 // Derived class of Derived1 class
{
int derived2_value;
public:
void Derived2_input()
{
cout<<"Enter the integer value of the second derived class: ";
cin>>derived2_value;
}
void sum()
{
cout << "The sum of the three intger values is: " << base_value +
derived1_value + derived2_value<<endl;
}
};
void main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl;
Derived2 d2;
clrscr();
cout<<"\n\t\t\tMULTILEVEL INHERITANCE";
cout<<"\n\t\t\t-----------------------\n\n\n";
d2.Base_input();
d2.Derived1_input();
d2.Derived2_input();
d2.sum();
getch();
}
Output:
MULTILEVEL INHERITANCE
RESULT:
Thus the program has been completed successfully.
Program:
#include <iostream.h>
#include<conio.h>
class A
{
public:
int x, y;
void A_input();
{
cout<<"\nEnter two values of class A: ";
cin>>x>>y;
}
};
class B : public A
{
public:
void product()
{
cout<<"\nThe Product of the two values is: "<< x * y<<endl;
}
};
class C : public A
{
public:
void division()
{
cout<<"\nThe Division of the two values is: "<< x / y<<endl;
}
};
void main()
{
cout<<"Welcome to DataFlair tutorials!"<<endl<<endl;
B b;
C c;
clrscr();
cout<<"\n\t\t\tHIERARCHICAL INHERITANCE";
cout<<"\n\t\t\t ";
b.A_input();
b.product();
c.A_input();
c.division();
getch();
}
Output:
HIERARCHICAL INHERITANCE
RESULT:
Thus the program has been completed successfully