Sie sind auf Seite 1von 25

BASIC PROGRAMS FOR C++

EX.NO:1.A FUNCTION WITH DEFAULT ARGUMENTS


DATE :
AIM:
To write a C++ program to find the sum for the given variables using function
with default arguments.
ALGORITHM:
1) Start the program.
2) Declare the variables and functions.
3) ive the values for two arguments in the function declaration itself.
!) Call function sum") with three values such that it ta#es one default arguments.
$) Call function sum") with two values such that it ta#es two default arguments.
%) Call function sum") with one values such that it ta#es three default arguments
$) &nside the function sum")' calculate the total.
%) (eturn the value to the main") function.
)) Displa* the result.
+) Stop the program.
SOURCE CODE:
,include-iostream.h.
,include-conio.h.
void main")
/
float sum"float a'int b011'int c01$'int d021)2
int a02'b03'c0!'d0$2
clrscr")2
cout--34nsum03--sum"1)2
cout--34nsum03--sum"a'b'c'd)2
cout--34nsum03--sum"a'b'c)2
cout--34nsum03--sum"a'b)2
cout--34nsum03--sum"a)2
cout--34nsum03--sum"b'c'd)2
getch")2
5
float sum"float i' int 6' int #' int l)
/
return"i+6+#+l)2
5
Output:
sum0!$
sum01!
sum027
sum0!1
sum0!)
sum032
RESULT:
Thus' the given program for function with default arguments has been
written and e8ecuted successfull*.
EX.NO:1.B IMPLEMENTATION OF CALL BY VALUE
DATE :
AIM:
To write a C++ program to find the value of a number raised to its power that
demonstrates a function using call b* value.
ALGORITHM:
1) Start the program.
2) Declare the variables.
3) et two numbers as input
!) Call the function power to which a cop* of the two variables is passed .
$) &nside the function' calculate the value of 8 raised to power * and store it in p.
%) (eturn the value of p to the main function.
)) Displa* the result.
+) Stop the program.
SOURCE CODE:
,include-iostream.h.
,include-conio.h.
void main")
/
int 8'*2
double power"int' int)2
clrscr")2
cout--39nter 8'*:3--endl2
cin..8..*2
cout--8--3 to the power 3--* --3 is 3-- power"8'*)2
getch")2
5
double power"int 8'int *)
/
double p2
p01.12
if"*.01)
while"*;;)
p<082
else
while"*++)
p=082
return"p)2
5
Output:
9>T9( ? ' @:
2 3
2 TA TB9 CAD9( 3 &S +
RESULT:Thus' the given program for implementation of call b* value has been written
and e8ecuted successfull*.
EX. NO: 1.C
IMPLEMENTATION OF CALL BY ADDRESS
AIM :
To write a c++ program and to implement the concept of Call b* Eddress
ALGORITHM:
1. Start the program
2. &nclude suitable header file
3. Declare a function swap with two pointes variables arguments
!. Declare and initialiFe the value as two variable in main")
$. Crint the value of two variable before swapping
%. Call the swap function b* passing address of the two variable as arguments
). Crint the value of two variable after swapping
+. Stop the program
CODING:
,include-iostream.h.
,include-conio.h.
void swap"int <8'int <*)2
int main")
/
clrscr")2
int i'62
i0112
60212
cout--34n the value of i before swapping is:3--i2
cout--34n the value of i before swapping is:3--62
swap "Gi'G6)2
cout--34n the value of i after swapping is:3--i2
cout--34n the value of i after swapping is:3--62
getch")2
return"1)2
5
void swap"int <8'int<*)
/
int temp0<82
<80<*2
<*0temp2
5
OUTPUT:
The value of i before swapping is: 21
The value of 6 before swapping is: 11
The value of i after swapping is: 11
The value of 6 after swapping is: 21
RESULT:
Thus to write a c++ program and to implement the concept of Call b* Eddress
was successfull* completed.
EX. NO:2.B
COPY CONSTRUCTORS
AIM :
To write the c++ program and to implement the concept of cop* constructor
ALGORITHM:
1. Start the program
2. &nclude suitable header file
3. Declare constructor with empt* arguments and another 'ob6ect as arguments
!. &nitialiFe the value of a variable b* calling a constructor in main function
$. Essign the value of one ob6ect to another ob6ect
%. 98ecute the program
). Crint the statement
+. Stop the program
CODING:
,include-iostream.h.
,include-conio.h.
class code
/
int id2
public:
code")/5
code"int a)
/
id0a2
5
code"codeG8)
/
id08.id2
5
void displa*"void)
/
cout--id2
5
52
int main")
/
clrscr")2
code a"111)2
code b"a)2
code c0a2
code d2
d0a2
cout--34nid of a:32 a.displa*")2
cout--34nid of b:32 b.displa*")2
cout--34nid of c:32 c.displa*")2
cout--34nid of d:32 d.displa*")2
getch")2
return 12
5
OUTPUT:
id of E:111
id of H:111
id of C:111
id of D:111
RESULT:
Thus to write the c++ program and implement the concept of cop* constructor
was successfull* completed.
EX. NO: .A
OPERATOR OVERLOADING
AIM :
To write a c++ program to concatenate two string using the concept of operator
Averloading.

ALGORITHM:
1. Start the program
2. &nclude suitable header file
3. Create a class with necessar* variables and function
!. et the value as two variables as concatenating
$. Averload an operator to concatenate two string
%. 98ecute the program
). Stop the program
CODING:
,include-iostream.h.
,include-string.h.
,include-conio.h.
const int bufsiFe0$12
class string
/
private:
char strIbufsiFeJ2
public:
string")
/
strcp*"str'3 3)2
5
string"char<m*str)
/
strcp*"str'm*str)2
5
void echo")
/
cout--str2
5
string operator+"string s)
/
string temp0str2
strcat"temp.str's.str)2
return temp2
5
52
void main")
/
clrscr")2
string str103 stud32
string str203 ent32
string str32
cout--34n before str30str10str22.32
cout--34n str1032
str1.echo")2
cout--34nstr2032
str2.echo")2
cout--34n str3032
str3.echo")2
str30str1+str22
cout--34n4n after str30str1+str2:..32
cout--34n str1032
str1.echo")2
cout--34n str2032
str2.echo")2
cout--34n str3032
str3.echo")2
getch")2
5
OUTPUT:
Hefore str30str10str22
str10stud
str20ent
str30
Efter
str30str1+str2:
str10stud
str20ent
str30student
RESULT:
Thus to write a c++ program to concatenate two string using the concept of
operator overloading was successfull* completed.
EX.NO: .B UNARY OPERATOR OVERLOADING
DATE :
AIM:
To perform increment and decrement operations using unar* operator
overloading.
ALGORITHM:
1) Start the program.
2) Create Class unar* with one constructors.
3) The constructor ta#es no arguments and is used to create ob6ects that are
not initialised.
!) Declare a function operator K++L and K;; inside the class unar* which are the
function where overloading is done.
$) Create an ob6ect for the class.
%) Call the functions with declared ob6ect.
)) Stop the program.
SOURCE CODE:
,include-iostream.h.
,include-conio.h.
class unar*
/
private:
int 8'*'F2
public:
unar*"void)
/
cout-- 39nter En* Three &nteger >os. : 32
cin..8..*..F2
5
void displa*"void)
/
cout-- endl-- 3 The Three >os. Ere : 3-- 8-- 3 ' 3-- *-- 3 ' 3-- F2
5
void operator ;;")
/
8 0 ;;82
* 0 ;;*2
F 0 ;;F2
5
void operator ++")
/
8 0 ++82
* 0 ++*2
F 0 ++F2
5
52
void main")
/
clrscr")2
unar* s2
s.displa*")2
;;s2
cout-- endl-- endl-- endl-- 3<<<<<<< The Decremented Malues <<<<<<<<3-- endl2
s.displa*")2
++s2
cout-- endl-- endl-- endl-- 3<<<<<<< The &ncremented Malues <<<<<<<<3-- endl2
s.displa*")2
cout-- endl-- endl-- 3<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<32
getch")2
5
Output:
9nter En* Three &nteger >os. :
! ) 7
The Three >os. Ere : ! ' ) ' 7
<<<<<<< The Decremented Malues <<<<<<<<
The Three >os. Ere : 3 ' % ' +
<<<<<<< The &ncremented Malues <<<<<<<<
The Three >os. Ere : ! ' ) ' 7
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
RESULT:
Thus' the given program for unar* operator overloading has been written and
e8ecuted successfull*.
EX. NO:.C
OVERLOADING BINARY OPERATOR
AIM :
To write a c++ program to implement the concept Hinar* operator overloading.
ALGORITHM:
1. Start the program
2. &nclude suitable header file
3. Create a class with necessar* variables and function
!. et the value as two variables
$. Averload an binar* operator
%. 98ecute the program
). Stop the program
CODING:
,include -iostream.h.
,include-conio.h.
class comple8
/
float 82
float *2
public:
comple8")/5
comple8"float real'float image)
/
80real2
*0image2
5
comple8 operator + "comple8)2
void displa*"void)2
52
comple8 comple8::operator+"comple8 c)
/
comple8 temp2
temp.808+c.82
temp.*0*+c.*2
return"temp)2
5
void comple8::displa*"void)
/
cout--8--3+63--*2
5
int main")
/
clrscr")2
comple8 c1'c2'c32
c10comple8"2.$'3.$)2
c20comple8"1.%'2.))2
c30c1+c22
cout--3c1 032
c1.displa*")2
cout--3c2 032
c2.displa*")2
cout--3c3 032
c3.displa*")2
getch")2
return 125
OUTPUT:
c102.$+63.$
c201.%+62.)
c30!.1+6%.2
RESULT:
Thus to write a c++ program to concatenate two string using the concept
Hinar* operator overloading was successfull* completed.
EX. NO:.D
FUNCTION OVERLOADING
AIM :
To write a c++ program to find the volume using function overloading concept

ALGORITHM:
1. Start the program
2. &nclude suitable header file
. Declare the function of same name with different parameter
!. Define the function for calculating the volume
". Call the respective functions
#. Crint the volume
$. stop the program
CODING:
,include-iostream.h.
,include-conio.h.
int volume"int)2
double volume"double'int)2
long volume"long'int'int)2
int main")
/
clrscr")2
cout--Nvolume of cube0N2
cout--volume"11)--34n32
cout--Nvolume of c*linder0N2
cout--volume"2.$'+)--34n32
cout--Nvolume of cubiod0N2
cout--volume"111')$'1$)--34n32
return 12
getch")2
5
int volume"int s)
/
return"s<s<s)2
5
double volume"double r'int h)
/
return"3.1!<r<r<h)2
5
long volume"long l'int b'int h)
/
return"l<b<h)25
OUTPUT:
volume of cube 0 1111
volume of c*linder 0 1$)11
volume of cubiod 0 112$11
RESULT:
Thus to write a c++ program to find the volume using function overloading concept was
successfull* completed.
EX. NO:!.A
MULT IPLE INHERITANCE
AIM
To write a c++ program to implement the concept of multiple inheritance.
ALGORITHM
1. Start the program
2. &nclude the suitable header file
3. create a base class O and >
!. Declare a variable m and void getm function in class O
$. Declare a variable n and void getn function in class >
%. Derive a class C from the base class OG> and declare a function void displa*
). Define the function void getm 'void getn and void displa* outside their respective class
+. et the value for the variable in getm and getn function
7. Call the displa* function for displa*ing the output
11. Stop the program
CODING:
,include-iostream.h.
class O
/
protected:
int m2
public:
void getm"int)2
52
class >
/
protected:
int n2
public:
void getn"int)2
52
class C:public O'public >
/
public:
void displa*"void)2
52
void O::getm"int 8)
/
m082
5
void >::getn"int *)
/
n0*2
5
void C::displa*"void)
/
cout--3m03--m--34n32
cout--3n03--n--34n32
cout--3m<n03--m<n--34n32
5
int main")
/
C p2
p.getm"11)2
p.getn"21)2
p.displa*")2
return 12
5
OUTPUT:
m011
n021
m<n0211
RESULT:
Thus to write a c++ program for multiple inheritance was successfull* completed.
EX. NO:!.B
HYBRID INHERITANCE
AIM:
To write a c++ program to implement the concept of h*brid inheritance.
ALGORITHM:
1. Start the program
2. &nclude suitable header files
3. Create a base class student and sports
!. &n the base class student define the function void get number and put number
$. &n the base class sports define the function void get score and void put score
%. Derive a class test form base student and define the function get mar# and put mar#
). Derive a class result from test and sports class and define function void displa*
+. et the value for get number 'get mar#s and get score function through main function
7. Call the displa* function in class result
11. Stop the program
CODING:
,include-iostream.h.
class student
/
protected:
int rollPnumber2
public
void getPnumber"int a)
/
rollPnumber0a2
5
void putPnumber"void)
/
cout--3(AQQ >A:3--rollPnumber--34n32
5
52
class test :public student
/
protected:
float part1'part22
public:
void getPmar#s"float 8'float *)
/
part1082
part20*2
5
void putPmar#s"void)
/
cout--3mar#s obtined:3--34n3
--3part103--part1--34n3
--3part203--part2--34n32
5
52
class sports
/
protected:
float score2
public:
void getPscore"float s)
/
score0s2
5
void putPscore"void)
/
cout--3sports wt:3--score--34n4n32
5
52
class result:public test'public sports
/
float total2
public:
void displa*"void)2
52
void result::displa*"void)
/
total0part1+part2+score2
putPnumber")2
putPmar#s")2
putPscore")2
cout--3total score:3--total--34n32
5
int main")
/
result studentP12
studentP1.getPnumber"123!)2
studentP1.getPmar#s"2).$'33.1)2
studentP1.getPscore"%.1)2
studentP1.displa*")2
return 12
5
OUTPUT:
(oll no: 123!
Oar#s obtained:
Cart102).$
Cart2033
Total score: %%.$
RESULT:
Thus to write a c++ program for h*brid inheritance was successfull* completed.

Das könnte Ihnen auch gefallen