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

C++ LAB MANUAL 2024 (4)

The document contains a series of C++ programming exercises, each demonstrating different programming concepts such as factorial calculation, sum of numbers, odd/even checking, and various inheritance types. Each exercise includes the program code, sample output, and a result statement confirming successful execution. The exercises cover fundamental programming principles including classes, objects, operator overloading, and file handling.

Uploaded by

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

C++ LAB MANUAL 2024 (4)

The document contains a series of C++ programming exercises, each demonstrating different programming concepts such as factorial calculation, sum of numbers, odd/even checking, and various inheritance types. Each exercise includes the program code, sample output, and a result statement confirming successful execution. The exercises cover fundamental programming principles including classes, objects, operator overloading, and file handling.

Uploaded by

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

FACTORIAL OF A GIVEN NUMBER

EX.NO.: 1

AIM:Program to Find Factorial Of Given Number

PROGRAM:

#include<iostream.h>
#include<conio.h>
void main()
{
inti, n, f=1;
clrscr();
cout<<”\n \t Factorial Of Given Number”<<endl;
cout<<”\n Enter any positive number:”<<endl;
cin>>n;
for(i=1;i<=n;i++)
{
f=f*i;
}
cout<<”Factorial of given number is:”<<f<<endl;
getch();
}

OUTPUT:

Factorial Of Given Number


Enter any positive number:5
Factorial of given number is: 120

RESULT:

Thus the required output has been acquired successfully.

1
SUM OF TWO NUMBERS

EX.NO.: 2

AIM:Program To Find Sum Of Two Numbers

PROGRAM:

#include<iostream.h>
#include<conio.h>
void main()
{
int a, b, c;
clrscr();
cout<<” \n \t Sum Of Two Numbers”<<endl;
cout<<”\n Enter the two numbers:”<<endl;
cin>>a>>b;
c=a+b;
cout<<”\n The result is:”<<c<<endl;
getch();
}

OUTPUT:

Sum Of Two Numbers

Enter the two numbers:


10
20
The result is: 30

RESULT:

Thus the required output has been acquired successfully.

2
ODD OR EVEN

EX.NO.: 3

AIM :To Find Whether The Given Number Is Odd Or Even.

PROGRAM:

#include<iostream.h>
int main()
{
int n;
cout<<"enter an integer:";
cin>>n;
if(n%2==0)
{
cout<<n<<"is even";
}
else
{
cout<<n<<"is odd";
}
return 0;
}

OUTPUT:

Enter an integer:6
6 is even
Enter an integer:7
7 is odd

RESULT:

Thus the required output has been acquired successfully.

3
LARGEST AMONG THREE NUMBERS

EX.NO.: 4

AIM: To find the largest among three numbers in C++.

PROGRAM:

#include<iostream.h>
int main()
{
float n1,n2,n3;
cout<<"Enter three numbers:";
cin>>n1>>n2>>n3;
if((n1>=n2) && (n1>=n3))
{
cout<<"largest number:"<<n1;
}
else if((n2>=n1)&&(n2>=n3))
}
cout<"Largest number:"<<n2;
}
else
{
cout<<"Largest number:"<<n3;
}
return0;
}
OUTPUT:

Enter three numbers: 7 8 9


Largest Number: 9

RESULT:
Thus the required output has been acquired successfully.

4
CLASSES AND OBJECT WITH MEMBER FUNCTIONS,
CONSTRUCTORS AND DESTRUCTORS

EX.NO.: 5

AIM: Program to perform area calculation using classes and object with
member functions, constructors and destructors.

PROGRAM:

#include <iostream.h>
class Line
{
private:
double length, breadth;
public:

double area( )
{
return length * breadth;
}

Line()
{
cout << "Object is being created" << endl;
length=10;
breadth=20;
}

~Line()
{
cout << "Object is being deleted" << endl;
}
};
int main()
{
Line obj;
5
cout << "Area of square : " obj.area()<<endl;
return 0;
}

Output

Object is being created


Area of square : 200
Object is being deleted

RESULT:

Thus the required output has been acquired successfully.

6
ARRAY OF OBJECTS

EX.NO.: 6

AIM:Program to use concept of arrays of objects using classes and objects


and store &display employee details

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
Class emp
{
int eid;
double hra, pf, da, cca, nf, bp;
chare name[10];
public:
void getdata()
{
cout<<”enter name of the employee:”<<endl;
gets(name);
cout<<”enter employee id:”<<endl;
cin>>eid;
cout<<”enter employee basic pay:”<<endl;
cin>>bp;
}
void allow()
{
hra=(bp*10)/100;
pf=(bp*12)/100;
da=(bp*11)/100;
cca=(bp*8)/100;
np=(bp+hra+da+cca)-pf;
}
Void putdata();
{
7
cout<<”employee name is:”<<ename<<endl;
cout<<”employee id is:”<<eid<<endl;
cout<<”employee basic pay is:”<<bp<<endl;
cout<<”\nhra=”<<hra<<”\n da=”<<da<<”\n pf=”<<pf<<”\n
cca=”<<cca;
cout<<”\n net pay of employee is:”<<np<<endl;
}
};
void main()
{
clrscr();
int n;
emp e[100];
cout<<”\n \t Arrays Of Objects”;
cout<<”\n enter number of employees:”<<endl;
cin>>n;
for(inti=0;i<n;i++)
{
cout<<”entering the details of employee:”<<i+1<<endl;
e[i].getdata();
e[i].allow();
}
clrscr();
for(inti=0;i<n;i++)
{
cout<<”displaying details of employee:”<<i+1<<endl;
e[i].putdata();
cout<<endl;
}
getch();
}

8
OUTPUT:

Arrays Of Objects

enter number of employees: 1

entering the details of employee:1


enter name of the employee: xyz
enter employee id:200
enter employee basic pay:1000

displaying details of employee: 1

employee name is: xyz


employee id is:200
employee basic pay is:1000
hra=100
da=110
pf=120
cca=80
net pay of employee is:1170

RESULT:

Thus the required output has been acquired successfully.

9
FUNCTION OVERLOADING

EX.NO.: 7

AIM :Program to display data of different type using function overloading.

#include <iostream.h>
class printData
{
public:
void print(int i)
{
cout << "Printing int: " << i << endl;
}
void print(double f)
{
cout << "Printing float: " << f << endl;
}
void print(char* c)
{
cout << "Printing character: " << c << endl;
}
};

int main()
{
printData pd;
pd.print(5);
pd.print(500.263);
pd.print("Hello C++");
return 0;
}

10
OUTPUT:

Printing int: 5
Printing float: 500.263
Printing character: Hello C++

RESULT:

Thus the required output has been acquired successfully.

11
FRIEND FUNCTION

EX.NO.: 8

AIM :Program to display sum of two numbers using friend function.

#include <iostream.h>
class B;

class A
{
private:
int numA;
public:
A(): numA(12) { }
friend int add(A, B);
};

class B
{
private:
int numB;
public:
B(): numB(1) { }
friend int add(A , B);
};

int add(A objectA, B objectB)


{
return (objectA.numA + objectB.numB);
}

12
int main()
{
A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}

OUTPUT:

Sum: 13

RESULT:

Thus the required output has been acquired successfully.

13
SINGLE INHERITANCE

EX.NO.: 9

AIM:Program To Use Concept Of Single Inheritance Using Classes And


Objects

PROGRAM:

#include<iostream.h>
#include<conio.h>

class staff
{
private:
char name[50];
int code;
public:
void getdata();
void display();
};

class typist: public staff


{
private:
int speed;
public:
void getdata();
void display();
};

14
void staff::getdata()
{
cout<<"Name:";
gets(name);
cout<<"Code:";
cin>>code;
}
void staff::display()
{
cout<<"Name:"<<name<<endl;
cout<<"Code:"<<code<<endl;
}
void typist::getdata()
{
cout<<"Speed:";
cin>>speed;
}
void typist::display()
{
cout<<"Speed:"<<speed<<endl;
}

int main()
{
typist t;
cout<<"Enter data"<<endl;
t.staff::getdata();
t.getdata();
cout<<endl<<"Display data"<<endl;
t.staff::display();
t.display();
getch();
return 0;
}

15
OUTPUT:

Enter data
Name:Roger Taylor
Code:13
Speed:46

Display data
Name:Roger Taylor
Code:13
Speed:46

RESULT:

Thus the required output has been acquired successfully.

16
MULTILEVEL INHERITANCE

EX.NO.: 10

AIM: Program to implement student result details using Multilevel


Inheritance.

PROGRAM:

#include<iostream.h>
class Student
{
protected:
int marks;
public:
void accept()
{
cout<<" Enter marks";
cin>>marks;
}
};

class Test :public Student


{
protected:
int p=0;
public:
void check()
{
if(marks>60)
{
p=1;
}
}
};

17
class Result :public Test
{
public:
void print()
{
if(p==1)
cout<<"\n You have passed";
else
cout<<"\n You have not passed";
}
};

int main()
{
Result r;

r.accept();
r.check();
r.print();

return 0;
}

OUTPUT:

Enter marks 70
You have passed

RESULT:

Thus the required output has been acquired successfully.

18
MULTIPLE INHERITANCE

EX.NO.: 11

AIM: Program to implement area and perimeter calculation for rectangle


using Multiple Inheritance.

PROGRAM:
#include <iostream.h>

class Area
{
public:
int getArea(int l, int b)
{
return l * b;
}
};

class Perimeter
{
public:
int getPerimeter(int l, int b)
{
return 2*(l + b);
}
};

class Rectangle : public Area, public Perimeter


{
int length;
int breadth;
public:
Rectangle()
{
length = 7;
19
breadth = 4;
}
int area()
{
return Area::getArea(length, breadth);
}
int perimeter()
{
return Perimeter::getPerimeter(length, breadth);
}
};

int main()
{
Rectangle rt;
cout << "Area : " << rt.area() << endl;
cout << "Perimeter : " << rt.perimeter() << endl;
return 0;
}

OUTPUT:

Multiple Inheritances
Area : 28
Perimeter : 22

RESULT:

Thus the required output has been acquired successfully.

20
HYBRID INHERITANCE

EX.NO.:12

AIM: Program to implement vehicle details using Hybrid Inheritance.

PROGRAM:

#include <iostream.h>
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};

class Fare
{
public:
Fare()
{
cout<<"Fare of Vehicle\n";
}
};

class Car: public Vehicle


{

};

class Bus: public Vehicle, public Fare


{

};
21
int main()
{
Bus obj2;
return 0;
}

OUTPUT:

This is a Vehicle
Fare of Vehicle

RESULT:

Thus the required output has been acquired successfully.

22
UNARY OPERATOR OVERLOADING

EX.NO.: 13

AIM:A program to overload unary operators.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class space
{
intx,y,z;
public:
voidgetdata(inta,intb,int c,);
void display();
void operator-();
};
void space::getdata(inta,intb,int c)
}
x=a;
y=b;
z=c;
}
void space::display()
{
cout<<x<<""<<y<<""<<z<<"\n";
}
void space::operator-()
{
x=-x;
y=-y;
z=-z;
}
void main()
{
23
space s;
cout<<"\n\t Overloading Of Unary Operator\n";
s.getdata(10,-20,30);
cout<<"s:";
s.display();
-s;
cout<<"s:";
s.display();
getch();
}

OUTPUT:

Overloading of unary operator

S: 10 -20 30

S: -10 20 -30

RESULT:

Thus required output has been acquired successfully.

24
BINARY OPERATOR OVERLOADING

EX.NO.: 14

AIM:A program to overload binary operators.

PROGRAM:

#include<iostream.h>
#include<conio.h>
class complex
{
floatx,y;
public:
complex()
{
}
complex(float real,floatimag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display();
};

complex complex: :operator+(complex c)


{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}

25
void complex::display()
{
cout<<x<<"+i"<<y<<"\n";
}

void main()
{
complex c1,c2,c3;
cout<<"\n\tOverloading Of Binary Operator \n";
c1=complex(2.5,3.5);
c2=complex(1.6,2.7);
c3=c1+c2;
cout<<"\nc1=";
c1.display();
cout<<"\nc2=";
c2.display();
cout<<"\nc3=";
c3.display();
getch();
}

OUTPUT:

Overloading Of Binary Operator


c1=2.5+i 3.5
c2=1.6+i 2.7
c3=4.1+i 6.2

RESULT:

Thus required output has been acquired successfully.

26
VIRTUAL FUNCTION

EX.NO.:15

AIM:A program to implement the concept of virtual function.

PROGRAM:

#include <iostream.h>
class Bird
{
public:
virtual void features()
{
cout << "Loading Bird features.\n";
}
};

class Parrot : public Bird


{
public:
void features()
{
this->Bird::features();
cout << "Loading Parrot features.\n";
}
};
class Penguin : public Bird
{
public:
void features()
{
this->Bird::features();
cout << "Loading Penguin features.\n";
}
};
class Loader
27
{
public:
void loadFeatures(Bird *bird)
{
bird->features();
}
};

int main()
{
Loader *l = new Loader;
Bird *b;
Parrot p1;
Penguin p2;

b = &p1;
l->loadFeatures(b);

b = &p2;
l->loadFeatures(b);

return 0;
}

OUTPUT:

Loading Bird features.


Loading Parrot features.
Loading Bird features.
Loading Penguin features.

RESULT:

Thus required output has been acquired successfully.

28
STREAM RELATED OPERATIONS

EX.NO.:16

AIM: Implement a program to write and read text in/from student file using
Stream related operations.

PROGRAM:

#include<fstream.h>
#include<conio.h>
class Student
{
int roll;
char name[25];
float marks;
void getdata()
{
cout<<"\n\nEnter Roll : "; cin>>roll;
cout<<"\nEnter Name : "; cin>>name;
cout<<"\nEnter Marks : "; cin>>marks;
}
void putdata()
{
cout<<"\n\t"<<roll<<"\t"<<name<<"\t"<<marks;
}
public:
void AddRecord()
{
fstream f;
Student Stu;
f.open("Student.dat",ios::app|ios::binary);
Stu.getdata();
f.write( (char *) &Stu, sizeof(Stu) );
f.close();
29
}
void Display()
{
fstream f;
Student Stu;
f.open("Student.dat",ios::in|ios::binary);
cout<<"\n\tRoll\tName\tMarks\n";
while( (f.read((char*)&Stu,sizeof(Stu))) != NULL )
Stu.putdata();
f.close();
}
};
void main()
{
Student S;
char ch='n';
do
{
S.AddRecord();
cout<<"\n\nDo you want to add another data (y/n) : ";
ch = getche();
} while(ch=='y' || ch=='Y');
cout<<"\nData written successfully...";
cout<<”\nStudent Database”;
S.Display();
}

OUTPUT:

Enter Roll : 1
Enter Name : Ashish
Enter Marks : 78.53

Do you want to add another data (y/n) : y

Enter Roll : 2
Enter Name : Kaushal
30
Enter Marks : 72.65

Do you want to add another data (y/n) : y

Enter Roll : 3
Enter Name : Vishwas
Enter Marks : 82.65

Do you want to add another data (y/n) : n

Data written successfully...

Student Database

Roll Name Marks


1 Ashish 78.53
2 Kaushal 72.65
3 Vishwas 82.65

RESULT:

Thus required output has been acquired successfully.

31
EXCEPTION HANDLING

EX.NO.: 17

AIM:Program to implement divide by zero exception using Exception


Handling mechanism.

PROGRAM:

#include<iostream.h>
#include<string.h>
int main()
{
int numerator,denominator,result;
cout<<"enter the numerator";
cin>>numerator;
cout<<"enter the denominator";
cin>>denominator;
try
{
if(denominator==0)
{
throw denominator;
}
result=numerator/denominator;
cout<<"the result of divison is :" <<result;
}
catch(int num)
{
cout<<"you cannot enter "<<num<<"in denominator";
}
}

32
OUTPUT:

ENTER THE NUMERATOR : 10


ENTER THE DENOMINATOR : 0

YOU CANNOT ENTER 0 IN DENOMINATOR

RESULT:

Thus the required output has been acquired successfully.

33
WRITE A PROGRAM TO IMPLEMENT PASSING STRING TO A
FUNCTION

EX.NO.: 18

AIM : Program to implement passing string to a function.

PROGRAM:

#include <iostream>

#include <sstream>

using namespace std;

string reverse( string s ) {

char temp;

int n = s.length();

for( int i = 0; i < n / 2; i++ ) {

temp = s[i];

s[i] = s[ n - i - 1 ];

s[ n - i - 1 ] = temp;

return s;

string isPalindrome( string s ) {

34
string revS = reverse( s );

if( s == revS ) {

return "True";

else {

return "False";

int main()

cout << "Is "racecar" a palindrome? " << isPalindrome( "racecar" ) <<
endl;

cout << "Is "abcdef" a palindrome? " << isPalindrome( "abcdef" ) << endl;

cout << "Is "madam" a palindrome? " << isPalindrome( "madam" ) <<
endl;

cout << "Is "sir" a palindrome? " << isPalindrome( "sir" ) << endl;

35
Output

Is "racecar" a palindrome? True

Is "abcdef" a palindrome? False

Is "madam" a palindrome? True

Is "sir" a palindrome? False

RESULT:

Thus the required output has been acquired successfully.

36
WRITE A PROGRAM TO CONCAT THE STRING

EX.NO.: 19

AIM : PROGRAM TO CONCAT THE STRING

PROGRAM:

#include <iostream>

#include <cstring>

using namespace std;

int main()

char key[25], buffer[25];

cout << "Enter the key string: ";

cin.getline(key, 25);

cout << "Enter the buffer string: ";

cin.getline(buffer, 25);

strcat(key, buffer);

cout << "Key = " << key << endl;

cout << "Buffer = " << buffer<<endl;

return 0;

37
Output:

Enter the key string: Welcome to

Enter the buffer string: C++ Programming.

Key = Welcome to C++ Programming.

Buffer = C++ Programming.

RESULT:

Thus the required output has been acquired successfully.

38
WRITE A PROGRAM TO PRINT A FULL STRING INPUT USING
KEYBOARD

EX.NO.: 20

AIM : Write a Program To Print A Full String Input Using Keyboard

PROGRAM:

#include<iostream>

#include<string.h>

using namespace std;

int main()

char a[20];

int i,j;

cout<<"\n\nEnter The String: ";

gets(a);

cout<<"\n\nYour Entered String is Given Below \n\n";

for(i=0;a[i]!='\0';++i)

cout<<a[i];

return 0;

39
Output

Enter The String: Studytonight

Your Entered String is Given Below

Studytonight

RESULT:

Thus the required output has been acquired successfully.

40

You might also like