0% found this document useful (0 votes)
7 views27 pages

C++ Practical Programs

The document is a laboratory manual for Object Oriented Programming using C++ at Adhiparasakthi College of Arts and Science. It includes various topics such as function overloading, classes and objects, passing objects to functions, friend functions, and operator overloading, along with corresponding algorithms and sample programs. Each section outlines the aim, algorithm, program code, and results of the exercises.

Uploaded by

d8750316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views27 pages

C++ Practical Programs

The document is a laboratory manual for Object Oriented Programming using C++ at Adhiparasakthi College of Arts and Science. It includes various topics such as function overloading, classes and objects, passing objects to functions, friend functions, and operator overloading, along with corresponding algorithms and sample programs. Each section outlines the aim, algorithm, program code, and results of the exercises.

Uploaded by

d8750316
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 27

OM SAKTHI

ADHIPARASAKTHI COLLEGE OF ARTS AND SCIENCE( (Autonomous)


G.B NAGAR, KALAVAI- 632506
DEPARTMENT OF COMPUTER SCIENCE AND APPLICATIONS

LABORATORY MANUAL
Prepared by
J.DEEPA M.SC(IT)., M.Phil.,B.Ed
ASSISTANT PROFESSOR

OBJECT ORIENTED PROGRAMMING


USING C++LAB
U1CS41MP
INDEX
Page
S.No. Date Topic Sign
No.
03-07-2023 Function Overloading , Default Argument and
1.
Inline function
2. 10-07-2023 Class and Object
3. 18-07-2023 Passing Objects to Function(call by value)
4. 24-07-2023 Friend Function
5. 31-07-2023 Passing Objects to Functions(call by reference)
6. 07-08-2023 Constructor and Destructor
7. 15-08-2023 Unary Operator
8. 21-08-2023 Binary Operator
9. INHERITANCE
28-08-2023 (a)Single inheritance
11-09-2023 (b)Multiple inheritance
19-09-2023 (c)Multilevel inheritance
25-09-2023 (d)Hierarchical inheritance
10. Virtual Function
11. Manipulate a Text File

12. Sequential I/O Operation on File


13. Find the Biggest Number using Command Line
Argument
14. Class Template
15. Function Template
16. Exception Handling
1.Function Overloading , Default Argument and Inline
function
Aim:
To implement Function overloading ,default arguments and inline function.

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>

void display(int varl, double var2=15.5)

cout << “\n Integer number: "<< varl;

cout << " \n double number: " << var2 << endl;

inline void display(double var)

cout << "Double number: " << var << endl;

void display(int var,int varl)

cout << “\n Integer number: " << var;

cout << "\n Integer number: " << varl;

void main( )

{
int a=5;

double b = 5.5;

clrscr( );

cout<<"\n\n\t\t\t\t function overloading\n";

display(a,8);

cout <<"\n\t\tusing default argument”;

display(a);

cout<<"\n\t\tusing inline function\n";

display(b);

getch( );

OUTPUT:
FUNCTION OVERLOADING

Integer number: 5

Integer number: 8

USING DEFAULT ARGUMENT

integer number:5

double number: 15.5

USING INLINE FUNCTION

Double number: 5.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()

return length * breadth;

double calculateVolume()

return length * breadth * height;

};

void main( )
{

Room room1;

clrscr( );

room 1.length = 12.5;

room 1.breadth = 30.8;

room 1.height = 19.2;

cout<<"\n\t\t\t USING CLASS AND OBJECT";

cout<<"\n\t\t\t—~—------------—\n";

cout<<"\n Calculate and Display Area & Volume of a Room";

cout << "\n\n Area of Room = " << room 1.calculate Area( );

cout << "\n Volume of Room = "<< room| .calculateVolume();

getch();

Output:
USING CLASS AND OBJECT

Calculate and Display Area & Volume of a Room

Area of Room =385

Volume of Room = 7392

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:

int length; public:

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.

5. Passing Objects to Function (call by reference)


AIM:
To implement passing objects to function(call by reference).

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:

Example program for PASS BY REFERENCE

Value of object1 before passing10

Value of object 2 before passing 20

Changed value of object1 100

Changed value of object2 200

Value of object1 after passing 100

Value of object2 after passing 200

RESULT:
Thus the program has been completed successfully.

6. Constructor and destructor


Aim:
To create a program for implement the concept of constructor and destructor.

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>

#include<conio.h> class Cube

public: int side; Cube()

side = 10;

Cube(int x)

side=x;

Cube(const Cube &c)

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

Default constructor :10

Parameterized constructor :20

Copy constructor :10

Destructor :

Destructor is called

Destructor is called

Destructor is called

RESULT:
Thus the program has been completed successfully.

7.unary operator overloading


AIM:
To complement the concept of unary operator overloading.
ALGORITHM:
step 1: start the program and declare the class distance.
step 2: Member functions are distance(),distance(int,int),displaydistance().
step 3: Overload the distance operator-() and distance operator++()
step 4: Main for can be declared and defined.
step 5: Create the object D1(11,-10)
step 6: Call the functions with help of objects.
step 7: Call the unary operator-D1 apply –negation
step 8: Call the unary operator++D1 apply –incremental operator
step 9: Call the display distance()
step 10: Stop the program.

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.

8.binary operator overloading


AIM: To implement the concept of binary operator overloading.

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

Distance operator+(Distance&, Distance&);


};

Distance operator+(Distance& d1, Distance& d2)


{
Distance d3;
d3.feet = d1.feet + d2.feet;
d3.inch = d1.inch + d2.inch;
return d3;
}
void main()
{
Distance d1(8, 9);
Distance d2(10, 2);
Distance d3;
clrscr();
cout<<"\n\t\t\t\tBINARY OPERATOR";
cout<<"\n\t\t\t\t---------------------\n\n\n";
cout<<"Before using Binary Operator the Value is :\n";
cout << "\n Feet : " <<d1.feet<<"\t"<<"Inches : " << d1.inch;
cout << "\n Feet : " <<d2.feet<<"\t"<<"Inches : " << d2.inch;
d3 = d1 + d2;
cout<<"\nUsing Binary Operator (+) Addition \n";
cout << "\n Feet: " <<d3.feet<<"\t"<<"Inches: " << d3.inch;
getch();
}
Output:

BINARY OPERATOR

Feet :8 inches :9

Feet :10 inches :2

Using binary operator (+)addition

Feet :18 inches :11_

RESULT:

Thus the program has been completed successfully.


9(A) Single inheritance
AIM: To create a program for implementing the concept of inheritance.

ALGORITHM:

step 1: Start the program.


step 2: Declare the class base.
step 3: Declare the data member and member function.
step 4: Create a derived class to inherit the base class.
step 5: Declare the data member and member function for derived class.
step 6: Declare and define the member function to display the result.
step 7: Main function can be declared and defined.
step 8: Create the object d.
step 9: Call the functions with help of object.
step 10: 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 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

Enter the integer value of base class:4

Enter the integer value of derived class:6

The sum of the two integer values is:10


RESULT:
Thus the program has been completed successfully.
9(b) multiple inheritance
AIM: To create a program for implementing the concept of multiple 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;
}
};

class Rectangle: public Shape, public PaintCost


{
public:
int getArea()
{
return (width * height);
}
};

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

Total print cost :$2450

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

Enter the integer value of base class:34

Enter the integer value of first derived class :78

Enter the integer value of the second derived class:12

The sum of the three integer values is:124

RESULT:
Thus the program has been completed successfully.

9(d) Hierarchical inheritance


AIM: To create a program for implementing the concept of hierarchical inheritance.
ALGORITHM:

step 1: Start the program.


step 2: Create a base class A.
step 3: Declare the member function and data member.
step 4: Create a derived class B that is inherited from base class A.
step 5: Declare the member function and data member.
step 6: Create a derived class C that is inherited fro base class A.
step 7: Declare the member function and data member.
step 8: Main function can be declared and defined.
step 9: Create the object b,c.
step 10: Call the functions with the help of object.
step 11: Display the result.

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

Enter two values of class A: 3 5

The product of two values is: 15

Enter two values of class A: 15 3

The division of the two values is: 5

RESULT:
Thus the program has been completed successfully

You might also like