Vous êtes sur la page 1sur 27

Ex.

No:1a:

STUDENT DATABASE USING CLASS

AIM:

To write a C++ program to maintain student database by using class concept.

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()

cout<<"\n Enter the Name:";

cin >> name;

cout << "\n Enter the Register Number:";

cin >> regno;

cout <<"\n Enter the Marks:";

cin>>m1>>m2>>m3;

void student :: calc()

sum=m1+m2+m3;

avg=sum/3;

void student::display()

cout << "\n Name of the Student:" << name;

cout << "\n Register number :"<<regno;

cout << "\n Mark1:" << m1;

cout << "\n Mark2:" << m2;

cout << "\n Mark3:" << m3;

cout << "\n Total Marks:" << sum;

cout << "\n Average:" << avg;

cout<<"\n------------------------------\n";

int main()

clrscr();

student s[size];

Page | 2
int n;

cout << "\n Enter the number of Students :";

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);

int area(int x,int y)

return(x * y);

Page | 5
}

float area(float x, float y)

return(0.5 * x * y);

int main()

clrscr();

int s,l,b;

float r,ba,h;

cout << " \nENTER SIDE OF SQUARE :\n";

cin >> s;

cout << "\nENTER THE RADIUS OF CIRCLE :\n";

cin >> r;

cout << "\nENTER THE LENGTH AND BREADTH OF RECTANGLE :\n";

cin >> l >> b;

cout << "\nENTER THE BASE AND HEIGHT OF TRIANGLE :\n";

cin >> ba >> h;

cout<<"\n\n";

cout <<" \nAREA OF SQUARE:" << area(s);

cout << "\nAREA OF CIRCLE :" << area(r);

cout << "\nAREA OF RECTANGLE :" << area(l,b);

cout << "\nAREA OF TRIANGLE :" << area(ba,h);

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()

cout << "\nENTER METRE VALUE :\n";

cin >> metre;

cout << "\nENTER CENTIMETER VALUE : \n";

cin >> centimetre;

friend void add(DM,DB);

};

class DB

int feet;

int inch;

public:

void setData()

cout << "\nENTER FEET VALUE :\n";

cin >> feet;

cout << "\nENTER INCH VALUE :\n";

cin >> inch;

friend void add(DM,DB);

};

void add(DM dm,DB db)

int ch;

while(1)

Page | 9
cout<<"\n ------------MENU------------\n";

cout <<"\n1.ADD IN METRE & CENTIMETRE" << "\n";

cout <<"\n2.ADD IN FEET & INCH " <<"\n";

cout <<"\n3.EXIT\n";

cin>>ch;

if(ch==1)

cout <<"\nDISPLAY IN METRE & CENTIMETRE\n\n";

int inch=db.feet*12 + db.inch;

int cm=dm.centimetre + inch * 2.54;

int m=cm/100+dm.metre;

cout<<"\t"<<m<<" Metre & " <<cm%100<<" Centimetre" <<"\n";

else if(ch == 2)

cout <<"\nDISPLAY IN FEET & INCH\n\n";

int cm=dm.metre*100 + dm.centimetre;

int inch=db.inch+cm/2.54;

int f=db.feet+inch/12;

cout<<"\t"<<f<< " feet & "<<inch%12<<" Inches:"<<"\n";

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;

friend complex add(complex,complex);

friend void display(complex);

};

complex add(complex A,complex B)

complex T;

T.r=A.r+B.r;

T.i=A.i+B.i;

return(T);

void display(complex C)

cout <<C.r <<"+j"<<C.i << "\n";

int main()

int x1,y1,z1;

clrscr();

cout<<"Enter the first complex number:";

cin >> x1;

cout << "Enter the second complex number:";

cin >> y1 >> z1;

complex A,B,C;

A=complex(x1);

B=complex(y1,z1);

Page | 14
C=add(A,B);

cout << "First Complex Number=";

display(A);

cout << "Second Complex Number=";

display(B);

cout<<"Sum of two Complex Numbers=";

display(C);

getch();

return 0;

Page | 15
OUTPUT:

LOGIN: Sowmiya

RESULT:

Page | 16
Ex.No:1e

OPERATOR OVERLOADING

AIM:

To write a C++ program to perform string comparison by overloading ‘==’ operator.

ALGORITHM:

1. Initialize the string class by using dynamic constructors


2. The overloading function returns a value of ‘1’ if the strings are equal and ‘0’ if
the strings are different.
3. The overloading is invoked when the ‘== ‘operator is used with string class
objects and string comparison function is performed.

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:

To write a C++ program to process bank transactions using inheritance concept.

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

Vous aimerez peut-être aussi