CPP
CPP
CPP
(a)
EXPERIMENT 1
#include <iostream>
using namespace std;
double dist=10.0;
class Box
{
double width,length;
public:
friend void printArea( Box box );
void setWidth( double wid );
inline void setLength(double len)
{
length=len;
}
};
// Member function definition using Scope Resolution operator
void Box::setWidth( double wid )
{
width = wid;
}
void printArea( Box box )
{
/* Because printArea() is a friend of Box, it can
directly access any member of this class */
cout<<"Length of box: "<<box.length<<endl;
cout<<"Width of box: "<<box.width<<endl;
cout << "Area of box : " << (box.width)*(box.length) <<endl;
}
int main( )
{
Box box;
double dist=20.0;
//Another use of Scope Resolution operator
box.setWidth(::dist);//Value of global variable dist
box.setLength(dist);//Value of local variable dist
printArea( box );
return 0;
}
OUTPUT:
EXPERIMENT 2
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
class student
{ int roll;
string name,add,branch;
long unsigned int Mobno;
public:
void getdata();
void showdata();
};
void student::getdata()
{ int i;
cout<<"\n ROLL NUMBER:";
cin>>roll;
cout<<"\n ENTER THE NAME OF STUDENT :";
cin>>name;
cout<<"\n ENTER THE ADDRESS OF STUDENT :";
cin>>add;
cout<<"\n ENTER THE BRANCH OF STUDENT :";
cin>>branch;
cout<<"\n MOB NO. :";
cin>>Mobno;
}
void student::showdata()
{ cout<<endl;
cout<<"\n ROLL NUMBER : "<<roll;
cout<<"\n NAME : "<<name;
cout<<"\n ADDRESS : "<<add;
cout<<"\n MOB NO. : "<<Mobno;
cout<<"\n BRANCH :"<<branch;
}
int main()
{
student s[2]; //declaring object of class student
int i;
for(i=0;i<2;i++)
{
cout<<"\n ENTER DETAILS OF STUDENT "<<i+1<<endl;;
s[i].getdata();
}
for(i=0;i<2;i++)
{
cout<<"\n DETAILS OF ALL STUDENTS "<<i+1;
s[i].showdata();
}
return 0;
}
OUTPUT:
EXPERIMENT 3
#include<iostream>
using namespace std;
class Plot
{
int Length,Width;
public:
Plot() //constructor of class
{
Length=5;
Width=5;
cout<<" P1 initialised through Default constructor(Default
value is 5x5)..."<<endl;
}
Plot(int i,int j) //parameterized constructor
{
Length=i;
Width=j;
cout<<"P2 initialised through Parameterized
constructor..."<<endl;
}
Plot(Plot &p) //copy constructor
{
Length=p.Length;
Width=p.Width;
cout<<" P2_identical initialised through Copy
constructor..."<<endl;
}
~Plot() //destructor of class
{
cout<<" Destructing... "<<endl;
}
void showdata()
{
cout<<" Length : "<<Length<<endl;
cout<<" Width : "<<Width<<endl<<endl;
}
};
int main()
{
int l,w;
cout<<" Enter Length and Width for plot P2 : ";
cin>>l>>w;
Plot P1;
Plot P2(l,w);
Plot P2_identical(P2);
cout<<" Details of P1 : "<<endl;
P1.showdata();
cout<<" Details of P2 : "<<endl;
P2.showdata();
cout<<" Details of P2_identical : "<<endl;
P2_identical.showdata();
return 0;
}
OUTPUT:
EXPERIMENT 4
#include <iostream>
using namespace std;
class account //base class
{
char cust_name[20];
int acc_no;
char acc_type[20];
public:
void get_accinfo()
{
cout<<"\n\nEnter Customer Name :- ";
cin>>cust_name;
cout<<"Enter Account Number :- ";
cin>>acc_no;
cout<<"Enter Account Type :- ";
cin>>acc_type;
}
void display_accinfo()
{
cout<<"\n\nCustomer Name :- "<<cust_name;
cout<<"\nAccount Number :- "<<acc_no;
cout<<"\nAccount Type :- "<<acc_type;
}
};
class cur_acct : public account //derived class publically inherited
{
static float balance;
public:
void disp_currbal()
{
cout<<"\nBalance :- "<<balance;
}
void deposit_currbal()
{
float deposit;
cout<<"\nEnter amount to Deposit :- ";
cin>>deposit;
balance = balance + deposit;
}
void withdraw_currbal()
{
float penalty,withdraw;
cout<<"\n\nBalance :- "<<balance;
cout<<"\nEnter amount to be withdraw :-";
cin>>withdraw;
balance=balance-withdraw;
if(balance < 500)
{
penalty=(500-balance)/10;
balance=balance-penalty;
cout<<"\nBalance after deducting penalty : "<<balance;
}
else if(withdraw > balance)
{
cout<<"\n\nYou have to take permission for Bank Overdraft
Facility\n";
balance=balance+withdraw;
}
else
cout<<"\nAfter Withdrawl your Balance revels : "<<balance;
}
};
class sav_acct : public account //derived class publically inherited
{
static float savbal;
public:
void disp_savbal()
{
cout<<"\nBalance :- "<<savbal;
}
void deposit_savbal()
{
float deposit,interest;
cout<<"\nEnter amount to Deposit :- ";
cin>>deposit;
savbal = savbal + deposit;
interest=(savbal*2)/100;
savbal=savbal+interest;
}
void withdraw_savbal()
{
float withdraw;
cout<<"\nBalance :- "<<savbal;
cout<<"\nEnter amount to be withdraw :-";
cin>>withdraw;
savbal=savbal-withdraw;
if(withdraw > savbal)
{
cout<<"\n\nYou have to take permission for Bank Overdraft
Facility\n";
savbal=savbal+withdraw;
}
else
cout<<"\nAfter Withdrawl your Balance is : "<<savbal;
}
};
float cur_acct :: balance;
float sav_acct :: savbal;
int main()
{
cur_acct c1;
sav_acct s1;
cout<<"\nEnter S for saving customer and C for current a/c
customer\n\n";
char type;
cin>>type;
int choice;
if(type=='s' || type=='S')
{
s1.get_accinfo(); //member function of base class
while(1)
{
cout<<"\nChoose Your Choice\n";
cout<<"1) Deposit\n";
cout<<"2) Withdraw\n";
cout<<"3) Display Balance\n";
cout<<"4) Display with full Details\n";
cout<<"5) Exit\n";
cout<<" Choose Your choice:-";
cin>>choice;
switch(choice)
{
case 1 : s1.deposit_savbal();
break;
case 2 : s1.withdraw_savbal();
break;
case 3 : s1.disp_savbal();
break;
case 4 : s1.display_accinfo();
s1.disp_savbal();
break;
case 5 : goto end;
default: cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
else
{
c1.get_accinfo(); //member function of base class
while(1)
{
cout<<"\nChoose Your Choice\n";
cout<<"1) Deposit\n";
cout<<"2) Withdraw\n";
cout<<"3) Display Balance\n";
cout<<"4) Display with full Details\n";
cout<<"5) Exit\n";
cout<<" Choose Your choice:-";
cin>>choice;
switch(choice)
{
end:
case 1 : c1.deposit_currbal();
break;
case 2 : c1.withdraw_currbal();
break;
case 3 : c1.disp_currbal();
break;
case 4 : c1.display_accinfo();
c1.disp_currbal();
break;
case 5 : goto end;
default: cout<<"\n\nEntered choice is invalid,\"TRY AGAIN\"";
}
}
}
return 0;
}
OUTPUT:
EXPERIMENT 5
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
string name,n; int val;
void display()
{
ifstream ifs("list.txt");
while (ifs >> name >> val)
{
cout << left << setw(30) << name
<<right << setw(12) << val << endl;
}
}
void showno()
{
int flag=0;
ifstream ifs("list.txt");
cout<<" Enter name : ";
cin>>n;
while(ifs>>name>>val)
{
if(name==n)
{
cout<<" Name : "<<name<<" Ph no. : "<<val<<endl;
flag=1;
break;
}
}
if(flag==0)
cout<<" Not found "<<endl;
}
void showname()
{
int flag=0;
int no;
ifstream ifs("list.txt");
cout<<" Enter Ph no. : ";
cin>>no;
while(ifs>>name>>val)
{
if(no==val)
{
cout<<" Name : "<<name<<" Ph no. : "<<val<<endl;
flag=1;
break;
}
}
if(flag==0)
cout<<" Not found "<<endl;
}
int main()
{
display();
cout<<" \nSearching no. "<<endl;
showno();
cout<<" \nSearching name "<<endl;
showname();
}
OUTPUT: