C++ LAB MANUAL 2024 (4)
C++ LAB MANUAL 2024 (4)
EX.NO.: 1
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:
RESULT:
1
SUM OF TWO NUMBERS
EX.NO.: 2
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:
RESULT:
2
ODD OR EVEN
EX.NO.: 3
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:
3
LARGEST AMONG THREE NUMBERS
EX.NO.: 4
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:
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
RESULT:
6
ARRAY OF OBJECTS
EX.NO.: 6
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
RESULT:
9
FUNCTION OVERLOADING
EX.NO.: 7
#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:
11
FRIEND FUNCTION
EX.NO.: 8
#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);
};
12
int main()
{
A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;
}
OUTPUT:
Sum: 13
RESULT:
13
SINGLE INHERITANCE
EX.NO.: 9
PROGRAM:
#include<iostream.h>
#include<conio.h>
class staff
{
private:
char name[50];
int code;
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:
16
MULTILEVEL INHERITANCE
EX.NO.: 10
PROGRAM:
#include<iostream.h>
class Student
{
protected:
int marks;
public:
void accept()
{
cout<<" Enter marks";
cin>>marks;
}
};
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:
18
MULTIPLE INHERITANCE
EX.NO.: 11
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);
}
};
int main()
{
Rectangle rt;
cout << "Area : " << rt.area() << endl;
cout << "Perimeter : " << rt.perimeter() << endl;
return 0;
}
OUTPUT:
Multiple Inheritances
Area : 28
Perimeter : 22
RESULT:
20
HYBRID INHERITANCE
EX.NO.:12
PROGRAM:
#include <iostream.h>
class Vehicle
{
public:
Vehicle()
{
cout << "This is a Vehicle" << endl;
}
};
class Fare
{
public:
Fare()
{
cout<<"Fare of Vehicle\n";
}
};
};
};
21
int main()
{
Bus obj2;
return 0;
}
OUTPUT:
This is a Vehicle
Fare of Vehicle
RESULT:
22
UNARY OPERATOR OVERLOADING
EX.NO.: 13
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:
S: 10 -20 30
S: -10 20 -30
RESULT:
24
BINARY OPERATOR OVERLOADING
EX.NO.: 14
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();
};
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:
RESULT:
26
VIRTUAL FUNCTION
EX.NO.:15
PROGRAM:
#include <iostream.h>
class Bird
{
public:
virtual void features()
{
cout << "Loading Bird features.\n";
}
};
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:
RESULT:
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
Enter Roll : 2
Enter Name : Kaushal
30
Enter Marks : 72.65
Enter Roll : 3
Enter Name : Vishwas
Enter Marks : 82.65
Student Database
RESULT:
31
EXCEPTION HANDLING
EX.NO.: 17
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:
RESULT:
33
WRITE A PROGRAM TO IMPLEMENT PASSING STRING TO A
FUNCTION
EX.NO.: 18
PROGRAM:
#include <iostream>
#include <sstream>
char temp;
int n = s.length();
temp = s[i];
s[i] = s[ n - i - 1 ];
s[ n - i - 1 ] = temp;
return 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
RESULT:
36
WRITE A PROGRAM TO CONCAT THE STRING
EX.NO.: 19
PROGRAM:
#include <iostream>
#include <cstring>
int main()
cin.getline(key, 25);
cin.getline(buffer, 25);
strcat(key, buffer);
return 0;
37
Output:
RESULT:
38
WRITE A PROGRAM TO PRINT A FULL STRING INPUT USING
KEYBOARD
EX.NO.: 20
PROGRAM:
#include<iostream>
#include<string.h>
int main()
char a[20];
int i,j;
gets(a);
for(i=0;a[i]!='\0';++i)
cout<<a[i];
return 0;
39
Output
Studytonight
RESULT:
40