Sie sind auf Seite 1von 14

1.

BASIC PROGRAMS FOR C++

EX.NO:1.1 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) Give the values for two arguments in the function declaration itself.
4) Call function sum() with three values such that it takes one default arguments.
5) Call function sum() with two values such that it takes two default arguments.
6) Call function sum() with one values such that it takes three default arguments
5) Inside the function sum(), calculate the total.
6) Return the value to the main() function.
7) Display the result.
8) Stop the program.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
void main()
{
float sum(float a,int b=10,int c=15,int d=20);
int a=2,b=3,c=4,d=5;
clrscr();
cout<<"\nsum="<<sum(0);
cout<<"\nsum="<<sum(a,b,c,d);
cout<<"\nsum="<<sum(a,b,c);
cout<<"\nsum="<<sum(a,b);
cout<<"\nsum="<<sum(a);
cout<<"\nsum="<<sum(b,c,d);
getch();
}
float sum(float i, int j, int k, int l)
{
return(i+j+k+l);
}
Output:

sum=45
sum=14
sum=29
sum=40
sum=47
sum=32

RESULT:
Thus, the given program for function with default arguments has been
written and executed successfully.
EX.NO:1.2 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 by value.

ALGORITHM:

1) Start the program.


2) Declare the variables.
3) Get two numbers as input
4) Call the function power to which a copy of the two variables is passed .
5) Inside the function, calculate the value of x raised to power y and store it in p.
6) Return the value of p to the main function.
7) Display the result.
8) Stop the program.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
void main()
{
int x,y;
double power(int, int);
clrscr();
cout<<"Enter x,y:"<<endl;
cin>>x>>y;
cout<<x<<" to the power "<<y <<" is "<< power(x,y);
getch();
}
double power(int x,int y)
{
double p;
p=1.0;
if(y>=0)
while(y--)
p*=x;
else
while(y++)
p/=x;
return(p);
}
Output:

ENTER X , Y:

2 3

2 TO THE POWER 3 IS 8

RESULT:
Thus, the given program for implementation of call by value has been
written and executed successfully.
EX.NO:1.3 CLASS WITH STATIC MEMBER FUNCTIONS
DATE :

AIM:
To implement a class with Static member functions.

ALGORITHM:
1) Start the program.
2) Create the Class customer with two member functions and one non member
function static.
3) Define the member functions outside the class.
4) Create an objects ‘c1’ and ‘c2’ for already created class item.
5) Call the member functions by using dot operator.
6) Stop the program.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
class customer
{
private:
static int count;
int accountno;
public:
void setaccountno(void);
void displayaccountno(void);
void static displaycount(void);
};
int customer::count;
void customer::setaccountno(void)
{
count++;
accountno=count;
}
void customer::displayaccountno(void)
{
cout<< "The Account Number Is:"<< accountno<< endl;
}
void customer::displaycount(void)
{
cout<< "The Total Numer Of Accounts Are:"<< count<< endl;
}
void main()
{
customer c1,c2;
clrscr();
c1.setaccountno();
c2.setaccountno();
c1.displayaccountno();
c2.displayaccountno();
c1.displaycount();
getch();
}

Output:

THE ACCOUNT NUMBER IS: 1

THE ACCOUNT NUMBER IS: 2

THE TOTAL NUMER OF ACCOUNTS ARE: 2

RESULT:
Thus, the given program for class with static member function has been
written and executed successfully.
EX.NO:1.4 UNARY OPERATOR OVERLOADING
DATE :

AIM:
To perform increment and decrement operations using unary operator
overloading.

ALGORITHM:
1) Start the program.
2) Create Class unary with one constructors.
3) The constructor takes no arguments and is used to create objects that are
not initialised.
4) Declare a function operator ‘++’ and ‘-- inside the class unary which are the
function where overloading is done.
5) Create an object for the class.
6) Call the functions with declared object.
7) Stop the program.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>
class unary
{
private:
int x,y,z;
public:
unary(void)
{
cout<< "Enter Any Three Integer Nos. : ";
cin>>x>>y>>z;
}
void display(void)
{
cout<< endl<< " The Three Nos. Are : "<< x<< " , "<< y<< " , "<< z;
}
void operator --()
{
x = --x;
y = --y;
z = --z;
}
void operator ++()
{
x = ++x;
y = ++y;
z = ++z;
}
};
void main()
{
clrscr();
unary s;
s.display();
--s;
cout<< endl<< endl<< endl<< "******* The Decremented Values ********"<< endl;
s.display();
++s;
cout<< endl<< endl<< endl<< "******* The Incremented Values ********"<< endl;
s.display();
cout<< endl<< endl<< "***************************************";
getch();
}
Output:

Enter Any Three Integer Nos. :


4 7 9

The Three Nos. Are : 4 , 7 , 9

******* The Decremented Values ********

The Three Nos. Are : 3 , 6 , 8

******* The Incremented Values ********

The Three Nos. Are : 4 , 7 , 9

***************************************

RESULT:
Thus, the given program for unary operator overloading has been written
and executed successfully.
.
EX.NO:1.5 FUNCTION OVERLOADING
DATE :
AIM:
To Write a C++ program that shows Function Overloading.
ALGORITHM:
1) Start the program.
2) Call function volume() with one value.
3) Call function volume() with two values.
4) Call function volume() with three values.
5) Return the value to the main() function.
6) Display the result.
7) Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int volume(int);
double volume(double,int);
long volume(long,int,int);
void main()
{
clrscr();
cout<<"Area of cube="<<volume(10)<<"\n";
cout<<"Area of Cylinder ="<<volume(2.5,8)<<"\n";
cout<<"Area of Rectangle="<<volume(100L,75,15)<<"\n";
getch();
}
int volume(int s)
{
return(s*s*s);
}
double volume(double r,int h)
{
return(3.14519*r*r*h);
}
long volume(long l, int b, int h)
{
return(l*b*h);
}
Output:

Area of cube=1000

Area of Cylinder =157.2595

Area of Rectangle=112500

RESULT:
Thus, the given program for function overloading has been written and
executed successfully.
EX.NO:1.6 MULTILEVEL INHERITANCE
DATE :

AIM:
To Write a C++ program using multilevel inheritance.
ALGORITHM:
1) Start the program.
2) Create Base Class student with data members and two member functions..
3) The two member functions of the base class are defined inside the class.
4) Create the two derived classes with data members and member functions.
5) The member functions of these two derived classes are defined inside the class
itself.
6) Create an object for the class.
7) Call the functions with declared object.
8) Stop the program.

SOURCE CODE:

#include<iostream.h>
#include<conio.h>

class student // Base Class


{
protected:
int rollno;
char *name;
public:
void getdata(int b,char *n)
{
rollno = b;
name = n;
}
void putdata(void)
{
cout<< " The Name Of Student \t: "<< name<< endl;
cout<< " The Roll No. Is \t: "<< rollno<< endl;
}
};

class test:public student // Derieved Class 1


{
protected:
float m1,m2;
public:
void gettest(float b,float c)
{
m1 = b;
m2 = c;
}
void puttest(void)
{
cout<< " Marks In OOP Is \t: "<< m1<< endl;
cout<< " Marks In OS Is \t: "<< m2<< endl;
}
};
class result:public test // Derived Class 2
{
protected:
float total;
public:
void displayresult(void)
{
total = m1 + m2;
putdata();
puttest();
cout<< " Total Of The Two \t: "<< total<< endl;
}};
void main()
{
clrscr();
int x;
float y,z;
char n[20];
cout<< "Enter Your Name:";
cin>>n;
cout<< "Enter The Roll Number:";
cin>>x;
result r1;
r1.getdata(x,n);
cout<< "ENTER OBJECT ORIENTED PROGRAMMING MARKS:";
cin>>y;
cout<< "ENTER OPERATING SYSTEMS MARKS:";
cin>>z;
r1.gettest(y,z);
cout<< endl<< endl<< "************ RESULT **************"<< endl;
r1.displayresult();
cout<< "**********************************"<< endl;
getch();
}
Output:

Enter Your Name: RISHI


Enter The Roll Number: 53
ENTER OBJECT ORIENTED PROGRAMMING MARKS: 92
ENTER OPERATING SYSTEMS MARKS: 90

************ RESULT ******************

The Name Of Student : RISHI

The Roll No Is : 53
Marks In OOP Is : 92
Marks In OS Is : 90
Total Of The Two : 182
***************************************

RESULT:
Thus, the given program for multilevel inheritance has been written and
executed successfully.

Das könnte Ihnen auch gefallen