Vous êtes sur la page 1sur 5

EX.

NO: 2 Simple class design and object creation


A IM:
To write a C++ program to create inventory application using class concept.
A L G OR I ! M :
1. Start the program
2. Declare a class named as item with two data members.
3. Define member function getdata! to assign value for data member
". Define member function putdata! to retrieve value of data member.
#. $n main function create ob%ect for class item.
&. Call getdata! function to assign value.
'. Call putdata! function to retrieve value of member function.
(. Stop the program.
"rogram :
)include*iostream.h+
)include*conio.h+
class item
,
int number- float cost- public .
void getdataint a/float b!-
void putdatavoid!
,
cout**01nter the number .0**number**02n0-
cout**01nter the cost 0**cost**02n0-
3
3-
void item ..getdataint a/float b!
, number4a- cost4b-
3
int main!
,
int a- float b- item 5- clrscr!-
cout**02nob%ect 50**02n0-
5.getdata166/27.7!-
5.putdata!-
item y-
cout**0enter item no.2n0-
cin ++a-
cout**0enter item cost .2n0-
cin++b-
cout**02nob%ect y 0**02n0-
y.getdataa/b!- y.putdata!- getch!-
return 6-
3
EX.NO:# Implementation o$ d%namic memor% allocation
A IM:
To write a C++ program to copy one string to another string and concatenate one string with
another string using dynamic memory allocationconstructor!.
A L G OR I ! M :
1. Start the program.
2. Declare a class string / define constructor which assign value of data member at run time.
3. Define display ! member function to display string.
". Define destructor to release memory usage by ob%ect after its usage.
#. Define %oin member function to concatenate two string.
&. $n main function create ob%ect for string which invo8e constructor.
'. 9sing ob%ect invo8e %oin function and dynamic constructor.
(. Display the resultant string.
7. $nvo8e destructor.
16. Stop the program.
"rogram:
)include*iostream.h+
)include*string.h+
)include*conio.h+
int count46-
class string
,
char :name-
int length-
public.
string!
,
length46-
count++-
cout**02n ;o of ob%ects created2n0**count-
name4new char<length+1=-
3
stringchar :s!
,
length4strlens!-
name4new char<length+1=-
strcpyname/s!-
count++-
cout**02n no of ob%ects created2n0**count-
3
void displayvoid!
,
cout**02n String0**02t0**name**02n0-
3
void %oinstring >a/string >b!
,
length4a.length+b.length-
delete name-
name4new char<length+1=-
strcpyname/a.name!-
strcatname/b.name!-
3
?string!
,
cout**0no of ob%ects destroyed2n0**count-
count@@-
3 3-
int main!
,
clrscr!-
string name1 0Ab%ect 0!-
string name2 0Ariented 0!-
string name3 0Brogramming Cab0!-
string s1/s2-
s1.%oinname1/name2!-
s2.%oins1/name3!-
name1.display!-
name2.display!-
name3.display!-
s1.display!-
s2.display!-
getch!-
return 6-
3
EX.NO:#.&' (op% constr)ctor
A IM:
To write a C++ program to generate Dibonacci series using copy constructor concept.
A L G OR I ! M :
1. Start the program
2. Declare a class constructor/ copy constructor.
3. Define member function increment ! and display! member function
". $n main function create ob%ect for class item.
#. Essign the ob%ect to another ob%ect by invo8ing copy constructor.
&. Call increment! and display! member function to display fibbonacci series.
*. Stop the program
" RO G R A M
)include*iostream.h+
)include*conio.h+
class fibonacci
,
private.
unsigned long int f6/f1/fib-
public.
fibonacci!
,
f64@1-
f141-
fib4f6+f1-
3
fibonaccifibonacci >ptr!
,
f64ptr.f6-
f14ptr.f1-
fib4ptr.fib-
3
void increment!
,
f64f1-
f14fib-
fib4f6+f1-
3
void display!
,
cout**02n0**fib**02n0-
3
3-
void main!
,
clrscr!-
fibonacci number-
cout**0Dibonacci series0-
forint i46-i*16-i++!
,
number.display!- number.increment!-
3
getch!-
3
Sample o ) tp )t
Dibonnacci series
6 1 1 2 3 # ( 13 21 3"
E+: , Operator o-erloading t.ro)g. $riend $)nction
Aim:
To implement operator overloading and friend function in C++.
Alogorit.m:
1. Start the program.
2. Create ob%ects for class comple5.
3. Dor the ob%ect Fc1G and Fc2G / assign values to real and imag using unary operator overloading with !
operator.
". The real and imaginary part of two comple5 numbers are got from the user.
#. The values of c1 and c2 are added through binary operator overloading using friend function with F+G
operator.
&. The values of c1 and c2 are subtracted through binary operator overloading using friend function with
F+G operator.
'. The addition and subtraction value of the two comple5 numbers are displayed with the help of
display! function.
(. Stop the program.
"rogram:
)include*iostream.h+
)include*conio.h+
class comple5
,
private.
float real/imag-
public.
comple5!
,
real4imag46-
3
void getdata!
,
cout**01nter the real part0**endl-
cin++real-
cout**0enter the imaginary part0**endl-
cin++imag-
3
friend comple5 operator +comple5 c1/ comple5 c2!
,
comple5 c-
c.real4c1.real+c2.real-
c.imag4c1.imag+c2.imag-
returnc!-
3
friend comple5 operator @comple5 c1/ comple5 c2!
void disp!-
3-
void comple5..disp!
,
cout**real**0+i0**imag**0!0**endl-
3
comple5 operator @comple5 c1/ comple5 c2!
,
comple5 c-
c.real4c1.real@c2.real-
c.imag4c1.imag@c2.imag-
returnc!-
3
void main!
,
clrscr!-
comple5 c1/c2/c3-
cout**02n1nter the comple5 input 10**endl-
c1.getdata!-
cout**02n1nter the comple5 input 20**endl-
c2.getdata!-
c34c1+c2-
cout**0Eddition of two comple5 nos0**endl-
c3.disp!-
c34c1@c2-
cout**0Subtraction of two comple5 nos0**endl-
c3.disp!-
getch!-
return-
3

Vous aimerez peut-être aussi