Académique Documents
Professionnel Documents
Culture Documents
No:1a:
AIM:
ALGORITHM:
1. Define a class student with data members for name, register number, marks
for three subjects and average.
2. The class also has member functions to get the data, calculate the total and
average and display all the contents.
3. An array of objects is declared and is used for processing ‘n’ students, by
invocation of appropriate member function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
const size=25;
class student
char name[size];
int regno,m1,m2,m3;
float sum,avg;
public:
void getdata(void);
void calc(void);
void display(void);};
Page | 1
void student::getdata()
cin>>m1>>m2>>m3;
sum=m1+m2+m3;
avg=sum/3;
void student::display()
cout<<"\n------------------------------\n";
int main()
clrscr();
student s[size];
Page | 2
int n;
cin >> n;
for(int i=0;i<n;i++)
s[i].getdata();
cout<<"******************************";
cout<<"\nSTUDENT DATABASE";
cout<<"\n*****************************\n";
for(i=0;i<n;i++)
s[i].calc();
s[i].display();
getch();
return 0;
Page | 3
OUTPUT:
LOGIN : Sowmiya
RESULT:
Page | 4
Ex.No:1b
FUNCTION OVERLOADING
AIM:
To write a C++ program to find the area of square, circle, rectangle and triangle using
function overloading concept.
ALGORITHM:
1. Define the functions for calculating area with the same name and different sets of
argument lists for the four operations.
2. Invoke the appropriate function by differentiating by the arguments lists.
3. Display the results.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int area(int);
float area(float);
float area(float,float);
int area(int,int);
int area(int x)
return( x * x);
float area(float x)
return(3.14 * x * x);
return(x * y);
Page | 5
}
return(0.5 * x * y);
int main()
clrscr();
int s,l,b;
float r,ba,h;
cin >> s;
cin >> r;
cout<<"\n\n";
getch();
return 0;
Page | 6
OUTPUT:
LOGIN : Sowmiya
RESULT:
Page | 7
Ex.No:1c
FRIEND FUNCTIONS
AIM:
To write a C++ program to convert and add the members of two classes using friend
functions.
ALGORITHM:
1. Define a class DM, with data members for values in meter and centimeter and
member function to set the values.
2. Define a class DB, with data members for values in feet and inches and member
function to set the values.
3. A friend function is defined to perform addition operation either in meter and
centimeter or in feet and inches.
4. To perform addition in meter and centimeter, the feet and inches are converted to
centimeters and then added.
5. To perform addition in feet and inches, the meter and centimeter are converted to
inches and then added.
PROGRAM:
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
class DB;
class DM
int metre;
int centimetre;
Page | 8
public:
void setData()
};
class DB
int feet;
int inch;
public:
void setData()
};
int ch;
while(1)
Page | 9
cout<<"\n ------------MENU------------\n";
cout <<"\n3.EXIT\n";
cin>>ch;
if(ch==1)
int m=cm/100+dm.metre;
else if(ch == 2)
int inch=db.inch+cm/2.54;
int f=db.feet+inch/12;
else
break;
void main()
DB db;
DM dm;
clrscr();
Page | 10
dm.setData();
db.setData();
add(dm,db);
getch();
Page | 11
OUTPUT:
LOGIN : Sowmiya
RESULT:
Page | 12
Ex.No:1d
CONSTRUCTOR OVERLOADING
AIM:
To write a C++ program to perform complex number addition by initializing the complex
numbers using constructor overloading.
ALGORITHM:
1. Define a class complex with members for real part and imaginary part.
2. Initialize the complex numbers by using constructors with a single argument and a
constructor with two arguments.
3. Perform the complex addition by using a friend function.
4. Display the complex numbers.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
int r,i;
public:
complex(){}
~complex(){}
complex(int a)
r=a;
i=a;
Page | 13
complex(int a,int b)
r=a;
i=b;
};
complex T;
T.r=A.r+B.r;
T.i=A.i+B.i;
return(T);
void display(complex C)
int main()
int x1,y1,z1;
clrscr();
complex A,B,C;
A=complex(x1);
B=complex(y1,z1);
Page | 14
C=add(A,B);
display(A);
display(B);
display(C);
getch();
return 0;
Page | 15
OUTPUT:
LOGIN: Sowmiya
RESULT:
Page | 16
Ex.No:1e
OPERATOR OVERLOADING
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class string
{
char *p;
int len;
public:
string()
{
len=0;
p=0;
}
Page | 17
~string()
{
delete p;
}
string(const char *s);
friend int operator==(const string &s,const string &t);
};
string::string(const char *s)
{
len=strlen(s);
p=new char[len+1];
strcpy(p,s);
}
int operator==(const string &s,const string &t)
{
if(strcmp(s.p,t.p)==0)
return(1);
else
return(0);
}
int main()
{
char *c1,*c2;
c1=new char[10];
c2=new char[10];
clrscr();
cout<<"\nENTER THE FIRST STRING :\n ";
Page | 18
cin>>c1;
cout << "\nENTER THE SECOND STRING :\n";
cin>>c2;
string s1(c1);
string s2(c2);
cout<<"\nSTRINGS "<<c1<<" & "<<c2<<"\tARE\t";
if(s1 == s2)
cout << "EQUAL";
else
cout << "NOT EQUAL";
getch();
return 0;
}
OUTPUT:
LOGIN: Sowmiya
RESULT:
Expt.No:1f
Page | 19
BANK ACCOUNT USING INHERITANCE
AIM:
ALGORITHM:
1. Define the base class account with members for account holder name, account type and
account number and member functions to get the values and display them.
2. Derive a subclass for savings account, with a member for amount and member functions to
deposit, withdraw, calculate interest and display the balance.
3. Derive another subclass for current account, with a member for amount and member
functions to deposit, withdraw with a minimum balance retained or a penalty and to display
the balance.
4. According to savings account or current account perform the operations and display the
derails.
PROGRAM:
#include<iostream.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
const size=30;
class account
{
char name[size],acctype[size];
int accno;
public:
void getdata(void)
{
cout<<"\n ENTER THE DETAILS";
Page | 20
cout<<"\nENTER THE ACCOUNT HOLDER NAME :\n";
cin >> name;
cout << "\nENTER THE ACCOUNT NUMBER :\n";
cin >> accno;
cout << "\nENTER THE ACCOUNT TYPE(SAVING/CURRENT):\n";
cin>>acctype;
}
char *type(void)
{
return(acctype);
}
void display(void)
{
cout<<"\n...............................";
cout<<"\n \n ACCOUNT DETAILS \n";
cout<<"\n..................................\n";
cout<<"\n NAME OF ACCOUNT HOLDER:" << name;
cout<<"\n ACCOUNT NUMBER:" << accno;
cout<<"\n TYPE OF ACCOUNT:" << acctype;
}
};
class sav_acc:public account
{
float amt;
public:
void set_amt(void)
{
Page | 21
amt=0;
}
void deposit(void)
{
float dpt_amt;
cout<<"\nENTER THE AMOUNT TO BE DEPOSITED :\n";
cin>>dpt_amt;
amt+=dpt_amt;
cout<<"\nBALANCE AFTER DEPOSIT :\n" << amt;
}
void withdraw(void)
{
float wdr_amt;
cout<<"\nENTER THE AMOUNT TO WITHDRAW:\n";
cin >> wdr_amt;
if(wdr_amt > amt)
cout << "\nSORRY.YOU CAN'T WITHDRAW";
else
{
amt-=wdr_amt;
cout<<"\nBALANCE AFTER WITHDRAW :\n"<<amt;
}
}
void interest(void)
{
float int_rate;
cout<<"\nENTER THE RATE OF INTEREST :\n";
Page | 22
cin >>int_rate;
cout<<"\nBALANCE WITHOUT INTEREST :\n"<<amt;
amt+=amt*int_rate;
cout<<"\nBALANCE WITH INTEREST :\n"<<amt;
}
void sav_disp(void)
{
cout <<"\nBALANCE AMOUNT :"<<amt;
}
};
class cur_acc:public account
{
float amt;
public:
void set_amt(void)
{
amt=0;
}
void deposit(void)
{
float dpt_amt;
cout<<"\nENTER THE AMOUNT TO BE DEPOSITED :\n";
cin >> dpt_amt;
amt+=dpt_amt;
cout<<"\nBALANCE AFTER DEPOSIT :\n"<<amt;
}
void withdraw(void)
Page | 23
{
float wdr_amt;
cout <<"\nENTER THE AMOUNT TO WITHDRAW:\n";
cin >> wdr_amt;
if(wdr_amt > amt)
cout << "\nSORRY.YOU CAN'T WITHDRAW";
else
{
amt-=wdr_amt;
cout<<"\nBALANCE AFTER WITHDRAW :\n"<<amt;
if(amt < 500)
{
amt-=200;
cout<<"\nMINIMUM BALANCE NOT MAINTAINED ;
BALANCE AFTER PENALTY :\n"<<amt;
}
}
}
void cur_disp(void)
{
cout<<"\nBALANCE AMOUNT :"<<amt;
}
};
void main()
{
cur_acc ca;
sav_acc sa;
Page | 24
clrscr();
ca.getdata();
char *type=ca.type();
if(strcmp(type,"SAVING")==0)
{
int ch;
cout<<"\n********************************\n";
cout << " SAVING ACCOUNT";
cout<<"\n********************************";
cout<<"\nWHAT DO YOU WANT TO DO?";
cout << "\n1.DEPOSIT\n 2.WITHDRAW\n3.INTEREST
CALCULATION\n4.DISPLAY\n 5.EXIT\n";
sa.set_amt();
while(1)
{
cout<<"\nENTER YOUR CHOICE :\n";
cin>>ch;
switch(ch)
{
case 1: sa.deposit();break;
case 2: sa.withdraw();break;
case 3: sa.interest();break;
case 4: ca.display();
sa.sav_disp();
break;
case 5: exit(0);
default: cout<<"\nWRONG CHOICE";break;
}
Page | 25
}
}
else
{
int ch;
cout<<"\n************************\n";
cout << "\n CURRENT ACCOUNT\n";
cout<<"***************************";
cout<<"\nWHAT DO YOU WANT TO DO?";
cout << "\n1.DEPOSIT\n 2.WITHDRAW\n 3.DISPLAY\n 4.EXIT";
ca.set_amt();
while(1)
{
cout<<"\nENTER YOUR CHOICE :\n";
cin>>ch;
switch(ch)
{
case 1: ca.deposit();break;
case 2: ca.withdraw();break;
case 3: ca.display();ca.cur_disp();break;
case 4:exit(0);
default:cout<<"\nWRONG CHOICE";break;
}
}
}
OUTPUT:
Page | 26
LOGIN: Sowmiya
RESULT:
Page | 27