C++ Lab Manual - 1-29

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

1a. Program to demonstrate function overloading and Inline function.

Aim: To write a C++ program to demonstrate function overloading and Inline function.

Algorithm:
Step1:Start
Step2:Create function print differing in the parameter list and make it line
Step3:Call the function by passing appropriate arguments.
Step4:Stop

Program:
// Write a C++ program to demonstrate function overloading and Inline function.
#include<iostream>
using namespace std;
inline void print();
inline void print(char);
void print(char,int);

int main()
{
print();
cout<<"\n";
print('#');
cout<<"\n";
print('$',15);
return 0;
}
inline void print()
{
for(int i=1;i<=10;i++)
cout<<"*";
}
inline void print(char c)
{
for(int i=1;i<=10;i++)
cout<<c;

}
void print(char c,int n)
{
for(int i=1;i<=n;i++)
cout<<c;
}

Output:
**********
##########
$$$$$$$$$$$$$$$
Result:
Program to demonstrate function overloading and Inline function was executed successfully
1b. Program to demonstrate Default Arguments
Aim:
To write a C++ program to demonstrate Default Arguments

Algorithm:
Step1:Start
Step2:Create function with default arguments
Step3:Call the function by passing appropriate parameters
Step4:Stop

Program:
/*Program to demonstrate Default Arguments */
#include<iostream>
using namespace std;
void print(char='%',int=20);
int main()
{
print();
cout<<"\n";
print('#');
cout<<"\n";
print('$',15);
return 0;
}

void print(char c,int n)


{
for(int i=1;i<=n;i++)
cout<<c;
}

Output:
%%%%%%%%%%%%%%%%%%%%
####################
$$$$$$$$$$$$$$$

Result:
Program to demonstrate Default Arguments was executed successfully
2. Program to demonstrate Class and Objects
Aim:
To write a C++ program to demonstrate Class and Objects

Class Diagram:

Algorithm:
Step1:Start
Step2:Create a class account as per the class diagram.
Step3:Create an object of account class
Step4:Call the members function of the account class
Step5:Stop

Program:
//Program to demonstrate Class and Objects
#include<iostream>
using namespace std;
class account{
int custid;
float balance;
public:
void read();
void print();

};
void account::read()
{
cout<<"Enter the customerid and balance";
cin>>custid>>balance;
}
void account:: print()
{
cout<<"CustId is "<<custid<<"\n";
cout<<"balance is "<<balance<<"\n";
}
int main()
{
account acc1;
acc1.read();
acc1.print();
return 0;
}
Output:
Enter the customerid and balance 1234 6000
CustId is 1234
balance is 6000

Result:
Program to demonstrate Class and Objects was executed successfully
3. Program to demonstrate the concept of Passing Objects to Functions
Aim:
To write a C++ program to demonstrate the concept of Passing Objects to Functions

Class Diagram:

Algorithm:
Step1:Start
Step2:Create a class account with data members and member functions as shown in the class
diagram.
Step3:Create an object of account class.
Step4:Call the member function moneytransfer
Step5:Stop

Program:
/*Program to demonstrate the concept of Passing Objects to Functions*/
#include<iostream>
using namespace std;
class account{
int custid;
float balance;
public:
void read();
void print();
void moneytransfer(account&);
};
void account::read()
{
cout<<"Enter the customerid and balance";
cin>>custid>>balance;
}
void account:: print()
{
cout<<"CustId is "<<custid<<"\n";
cout<<"balance is "<<balance<<"\n";
}
void account::moneytransfer(account &a)
{
float amt;
cout<<"Enter the amount\n";
cin>>amt;
balance=balance-amt;
a.balance=a.balance+amt;
}
int main()
{
account acc1,acc2;
cout<<"Enter customer1 details\n";
acc1.read();
cout<<"Enter customer2 details\n";
acc2.read();
cout<<"Transferring money from customer1 to customer2\n";
acc1.moneytransfer(acc2);
acc1.print();
acc2.print();

return 0;
}

Output:
Enter customer1 details
Enter the customerid and balance 1234 7000
Enter customer2 details
Enter the customerid and balance 1235 8000
Transferring money from customer1 to customer2
Enter the amount
500
CustId is 1234
balance is 6500
CustId is 1235
balance is 8500

Result:
Program to demonstrate the concept of Passing Objects to Functions was executed successfully
4. Program to demonstrate the Friend Function.
Aim:
To write a C++ program to demonstrate the Friend Function.

Class Diagram:

Algorithm
Step1:Start
Step2:Create account class as per the class diagram
Step3:Create a function print and make it a friend of the account class
Step4:Create an object of account class
Step5:Call the functions
Step6:Stop

Program:
//Program to demonstrate the Friend Functions.
#include<iostream>
using namespace std;
class account{
int custid;
float balance;
public:
void read();
friend void print(account);

};
void account::read()
{
cout<<"Enter the customerid and balance";
cin>>custid>>balance;
}
void print(account a)
{
cout<<"CustId is "<<a.custid<<"\n";
cout<<"balance is "<<a.balance<<"\n";
}
int main()
{
account acc1;
acc1.read();
print(acc1);
return 0;
}
Output:
Enter the customerid and balance 1234 9000
CustId is 1234
balance is 9000

Result:
Program to demonstrate the Friend Functions was executed successfully
5. Program to demonstrate Constructor and Destructor
Aim:
To write a C++ program to demonstrate Constructor and Destructor

Class Diagram:

Algorithm:
Step1:Start
Step2:Create account class as per the class diagram
Step3:Create objects of account class by invoking the appropriate constructors
Step4:Call the function print to display
Step5:Stop

Program:
//Program to demonstrate Constructor and Destructor
#include<iostream>
using namespace std;
class account{
int custid;
float balance;
public:
account()
{
cout<<"Default constructor called\n";
custid =1111;
balance=2000;
}
account(int cid,float bal)
{
cout<<"Argument constructor called\n";
custid=cid;
balance=bal;
}
void print()
{
cout<<"CustId is "<<custid<<"\n";
cout<<"balance is "<<balance<<"\n";
}
~account()
{
cout<<"Destructor called\n";
}
};
int main()
{
account acc1;
account acc2(1234,4000);

acc1.print();
acc2.print();
return 0;
}

Output:
Default constructor called
Argument constructor called
CustId is 1111
balance is 2000
CustId is 1234
balance is 4000
Destructor called
Destructor called

Result:
Program to demonstrate Constructor and Destructor was executed successfully
6. Program to demonstrate Unary Operator Overloading
Aim:
To write a C++ program to demonstrate Unary Operator Overloading

Class Diagram:

Algorithm:
Step1:Start
Step2:Create class Idx as per the class diagram
Step3:Create object of Idx class
Step4:Call appropriate operator overloaded functions
Step5:Stop

Program:
/* Program to demonstrate Unary Operator Overloading */
#include<iostream>
using namespace std;
class Idx
{
int val;
public:
Idx(int v=0){
val=v;
}
void print()
{
cout<<val<<"\n";
}
Idx operator ++()
{
++val;
return *this;
}
Idx operator ++(int)
{
Idx temp;
temp.val=val;
val++;
return temp;
}

};
int main()
{
Idx obj1;
Idx obj2(4);
obj1=obj2++;//obj1=obj2.operator ++(int)
cout<<"obj1=obj2++ gives\n";
cout<<"obj1=";
obj1.print();
cout<<"obj2=";
obj2.print();
cout<<"obj1=++obj2 gives\n";
obj1=++obj2;//obj1=obj2.operator ++()
cout<<"obj1=";
obj1.print();
cout<<"obj2=";
obj2.print();
return 0;
}

Output:
obj1=obj2++ gives
obj1=4
obj2=5
obj1=++obj2 gives
obj1=6
obj2=6

Result:
Program to demonstrate Unary Operator Overloading was executed successfully
7. Program to demonstrate Binary Operator Overloading

Aim:
To write a C++ program to demonstrate Binary Operator Overloading

Class Diagram:

Algorithm:
Step1:Start
Step2:Create complex class as shown in the class diagram
Step3:Create object of complex class
Step4:Call the binary operator overloaded function
Step5:Stop

Program:
//Program to demonstrate Binary Operator Overloading
#include<iostream>
#include<math.h>
using namespace std;
class complex{
int real;
int imag;
public:
void read()
{
cout<<"\nEnter the real part";
cin>>real;
cout<<"Enter the imaginary part";
cin>>imag;
}
void print()
{
cout<<real;
if(imag>0)
cout<<"+i"<<imag;
else
cout<<"-i"<<abs(imag);
cout<<"\n";
}
complex operator +(complex c)
{
complex temp;
temp.real=real+c.real;
temp.imag=imag+c.imag;
return temp;
}

};

int main()
{
complex c1,c2,c3;
c1.read();
cout<<"Complex number c1=";
c1.print();
c2.read();
cout<<"Complex number c2=";
c2.print();
cout<<"c3=c1+c2 is ";
c3=c1+c2;
c3.print();
return 0;
}

Output:
Enter the real part3
Enter the imaginary part5
Complex number c1=3+i5

Enter the real part2


Enter the imaginary part6
Complex number c2=2+i6
c3=c1+c2 is 5+i11

Result:
Program to demonstrate Binary Operator Overloading was executed successfully
8a. Program to demonstrate Single Inheritance

Aim:
To write a C++ program to demonstrate Single Inheritance

Class Diagram:

Algorithm:
Step 1:Start
Step 2:Create class hierarchy as shown in the class diagram
Step 3:Create objects of parent class and child class
Step 4:Call the member functions and observe the results
Step 5:Stop

Program:
#include<iostream>
using namespace std;
class parent{
private:
int parentdata;
public:
void readparentdata()
{
cout<<"Enter parentdata\n";
cin>>parentdata;
}
void printparentdata()
{
cout<<"The parent data is "<<parentdata<<"\n";
}
};

class child:public parent


{
int childdata;
public:
void readchilddata()
{
readparentdata();
cout<<"Enter childdata\n";
cin>>childdata;
}
void printchilddata()
{
printparentdata();
cout<<"The child data is "<<childdata<<"\n";
}
};

main()
{
child c;
c.readchilddata();
c.printchilddata() ;
}
Output:
Enter parentdata
10
Enter childdata
20
The parent data is 10
The child data is 20

Result:
The program to demonstrate Single Inheritance was executed successfully
8b. Program to demonstrate Multilevel Inheritance

Aim:

To write a C++ program to demonstrate Multilevel Inheritance

Class Diagram:

Algorithm:
Step 1.Start
Step 2.Create class hierarchy as shown in the class diagram
Step 3.Create object of all the classes
Step 4.Call the member functions and observe the result
Step 5.Stop

Program:
#include<iostream>
using namespace std;
class emp
{
public:
int eno;
void get()
{
cout<<"enter the number";
cin>>eno;
}
void print()
{
cout<<"no"<<eno<<endl;
}
};
class man:public emp
{
protected:
char name[20];
long int sal;
public:
void get1()
{
cout<<"enter the name";
cin>>name;
cout<<"enter the salary";
cin>>sal;
}
void print1()
{
cout<<"the name is:"<<name<<endl;
cout<<"the salary is:"<<sal<<endl;
}
};
class fore:public man
{
public:
int quota;
void get2()
{
cout<<"enter the quota";
cin>>quota;
}
void print2()
{
cout<<"quota="<<quota<<endl;
}
};
main()
{
fore f;
f.get();
f.print();
f.get1();
f.print1();
f.get2();
f.print2();
}

Output:
enter the employee number 1234
eno 1234
enter the name Amrita
enter the salary 6000
the name is:Amrita
the salary is:6000
enter the quota 1
quota=1

Result:
The program to demonstrate Multilevel Inheritance was executed successfully
8c. Program to demonstrate Multiple Inheritance
Aim:
To write a C++ program to demonstrate Multiple Inheritance

Class Diagram:

Algorithm:
Step 1.Start
Step 2.Create class hierarchy as shown in the class diagram
Step 3.Create object of all the classes
Step 4.Call the member functions and observe the result
Step 5.Stop

Program:
#include<iostream>
using namespace std;

class emp
{
protected:
int eno,age;
float salary;
char name[30];
public:
void get()
{
cout<<"enter empno,name,age,salary"<<endl;
cin>>eno>>name>>age>>salary;
}
void disp()
{
cout<<"eno name age salary"<<endl;
cout<<eno<< name <<age<< salary<<endl;
}
};
class dept
{
protected:
int dno;
char dname[30];
public:
void get1()
{
cout<<"enter dept no dept name"<<endl;
cin>>dno>>dname;
}
void disp1()
{
cout<<"dept no,deptname"<<endl;
cout<<dno<<" "<<dname<<endl;
}
};

class labour:public emp,public dept


{
float hra,net;
public:
void getdata()
{
get();
get1();
cout<<"enter hra"<<endl;
cin>>hra;
net=salary+hra;
}
void dispdata()
{
disp();
disp1();
cout<<"hra netsalary"<<endl;
cout<<hra<<" "<<net<<endl;
}
};
main()
{
labour l;
l.getdata();
l.dispdata();
}

Output:
enter empno,name,age,salary
1234 amrita 23 8045
enter dept no dept name
2 admin
enter hra
2000
eno name age salary
1234 amrita 23 8045
dept no,deptname
2 admin
hra netsalary
2000 10045

Result:
The program to demonstrate Multiple Inheritance was executed successfully
8d. Program to demonstrate Hierarchical Inheritance

Aim:
To write a C++ program to demonstrate Hierarchical Inheritance

Class Diagram:

Algorithm:
Step 1.Start
Step 2.Create class hierarchy as shown in the class diagram
Step 3.Create pointer of shape class
Step 4.Pointer of shape class points to objects of circle,square and triangle class in random
order
Step 5.Call the overloaded virtual function draw() and observe the result
Step 6.Stop

Program:
#include<iostream>
#include<stdlib.h>
using namespace std;
class shape
{
public:
virtual void draw()=0;
};
class circle:public shape{
public:
void draw(){
cout<<"Inside circle class\n";
}
};
class square:public shape{
public:
void draw(){
cout<<"Inside square class\n";
}
};
class triangle:public shape{
public:
void draw(){
cout<<"Inside triangle class\n";
}
};
shape *create()
{
switch(rand()%3)
{
case 0:return new(circle);
case 1:return new(square);
case 2:return new(triangle);
}
}

main()
{
int i;
for(i=0;i<8;i++)
{
shape *ptr=create();
ptr->draw();
delete(ptr);
}
}

Output:
Inside triangle class
Inside triangle class
Inside square class
Inside square class
Inside triangle class
Inside square class
Inside circle class
Inside circle class

Result:
The program to demonstrate Hierarchical Inheritance was executed successfully
8e. Program to demonstrate Hybrid inheritance

Aim:

To write a C++ program to demonstrate Hybrid inheritance

Class Diagram:

Algorithm:
Step 1.Start
Step 2.Create class hierarchy as shown in the class diagram
Step 3.Create object of all the classes
Step 4.Call the member functions and observe the result
Step 5.Stop

Program:
#include <iostream>
using namespace std;
class colour {
public:
void display(){
cout<<"Display in colour class\n";
}
};

class redcolour:virtual public colour{


public:
void display(){
cout<<"Display in redcolour class\n";
}
};

class greencolour:virtual public colour{


public:
void display(){
cout<<"Display in greencolour class\n";

}};

class yellowcolour:public redcolour,public greencolour{


public:
void display(){
cout<<"Display in yellowcolour class\n";
}};

main()
{
yellowcolour obj;
obj.display();
obj.redcolour::display();
obj.greencolour::display();
obj.colour::display();
}

Output:
Display in yellowcolour class
Display in redcolour class
Display in greencolour class
Display in colour class

Result:

The program to demonstrate Hybrid inheritance was executed successfully


9. Program to demonstrate Virtual Function

Aim:
To write a C++ program to demonstrate Virtual Function

Class Diagram:

Algorithm:
Step 1:Start
Step 2:Create parent and child class as shown in the class diagram
Step 3:Create pointer to the base class
Step 4:Create an object of parent class
Step 5:Create an object of child class
Step 6:Store the address of the child object in the parent class pointer and then call display fn
using the pointer
Step 7:Stop

Program:
//Program to demonstrate Virtual Functions.
#include<iostream>
using namespace std;
class parent{
int i;
public:
virtual void display()
{
cout<<"Display function inside parent\n";
}
};
class child:public parent
{
int j;
public:
void display()
{
cout<<"Display function inside child\n";
}
};
int main()
{
parent *ptr;
parent pobj;
child cobj;
ptr=&pobj;
ptr->display();
ptr=&cobj;
ptr->display();
return 0;
}

Output:
Display function inside parent
Display function inside child

Result:
Program to demonstrate Virtual Functions was executed successfully
10. Program to manipulate a Text File.

Aim:
To write a C++ program to manipulate a Text File.

Algorithm:
Step1:Start
Step2:Create a file data3.txt in both read and write mode
Step3:Write the data to the file
Step4:Display the position of the file pointer
Step5:Move the file pointer to the beginning of the file
Step6:Display the contents of the file.
Step7:Move the file pointer to the 4th position and write the data
Step 8.Bring the file pointer back to the beginning of the file
Step 9.Display the contents of the file.
Step10:Stop

Program:
#include<iostream>
#include<fstream>
using namespace std;
main()
{
int a,b,c;
fstream fs("data3.txt",ios::in|ios::out);
fs<<123<<' '<<456<<' '<<789;
cout<<"No of bytes written"<<fs.tellg()<<"\n";
fs.seekg(0,ios::beg);
cout<<"file pointer is at byte "<<fs.tellg()<<"\n";
fs>>a>>b>>c;
cout<<a<<" "<<b<<" "<<c<<"\n";
cout<<"Changing contents of the file\n";
fs.seekg(4,ios::beg);
cout<<"file pointer is at byte "<<fs.tellg()<<"\n";
fs<<555;
fs.seekg(0,ios::beg);
fs>>a>>b>>c;
cout<<a<<" "<<b<<" "<<c<<"\n";
}

Output:
No of bytes written11
file pointer is at byte 0
123 456 789
Changing contents of the file
file pointer is at byte 4
123 555 789

Result:
The program to manipulate a Text File was executed successfully

You might also like