0% found this document useful (0 votes)
11 views45 pages

C++ Programs for Prime Numbers and More

The document outlines several C++ programming exercises, including finding prime numbers between two numbers, linear searching, calculating areas of geometric shapes using function overloading, and determining the largest of three numbers using normal and inline functions. It also covers student mark details using constructors, book details using friend functions, and volume calculations for geometric shapes through constructor overloading. Each exercise includes an aim, algorithm, source code, output examples, and results confirming successful execution.

Uploaded by

gowri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views45 pages

C++ Programs for Prime Numbers and More

The document outlines several C++ programming exercises, including finding prime numbers between two numbers, linear searching, calculating areas of geometric shapes using function overloading, and determining the largest of three numbers using normal and inline functions. It also covers student mark details using constructors, book details using friend functions, and volume calculations for geometric shapes through constructor overloading. Each exercise includes an aim, algorithm, source code, output examples, and results confirming successful execution.

Uploaded by

gowri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Ex.

No:1PRIME NUMBER BETWEEN TWO Date:


GIVEN NUMBERS
AIM:
To write a c++ program to print prime numbers between two given numbers.

ALGORITHM:
Step1: Start the program.
Step2: Declare the variables start,end,i,j,count=0.
Step3: Get the input values for prime number.
Step4: Print the input values.
Step5: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
int start,end,i,j,count=0;
clrscr();
cout<<"\n\t PRIME NUMBERS BETWEEN TWO GIVEN NUMBERS";
cout<<"\n\t *************************************************";
cout<<"\n\t INPUT";
cout<<"\n\t *****";
cout<<"\n Enter the Starting number:";
cin>>start;
cout<<"\n Enter the Ending number:";
cin>>end;
cout<<"\n\t OUTPUT";
cout<<"\n\t ********";
cout<<"\n\t Prime number between" <<start<< "and" <<end<< "is:\n";
for(i=start;i<=end;i++)
{
count=0;
for(j=2;j<i;j++)
{
if(i%j==0)
{
count++;
break;
}

1
}
if(count==0)
{
cout<<i<<" ";
}
}
getch();
}
OUTPUT:
PRIME NUMBER BETWEEN TWO GIVEN NUMBERS
*************************************************
INPUT
******
Enter the Starting number:3
Enter the Ending number:9
OUTPUT
********
Prime number between 3 &9is:
5,7

RESULT:
Thus the above program was executed successfully and output was verified.

[Link] LINEAR SEARCHINGDate:


AIM:
To write a c++ program to find searching elements.

ALGORITHM:
Step1: Start the program.
Step2: Get the class linear.
Step3: Declare the variables s,n,i,f,c,a[30] as integer data type.
Step4: Get the input value to assign c in function.

2
Step5: To check the for loop condition.
Step6: To check the if condition.
Step7: Check the ‘s’ value=0 it is true print the number not occur otherwise
occur in time.
Step8: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
void main()
{
int f,n,i,s,c,a[10];
clrscr();
cout<<"\n\t LINEAR SEARCHING";
cout<<"\n\t ********************";
cout<<”\n\t INPUT”;
cout<<”\n\t ******”;
cout<<"\n Enter the number of elements:";
cin>>n;
cout<<"\n Enter the elements one by one:";
for(i=1;i<=n;i++)
{
cin>>a[i];
}
cout<<"\n\t Enter the number to be searched:";
cin>>s;
cout<<"\n\t OUTPUT";
cout<<"\n\t ********";
c=0;
f=0;
for(i=1;i<=n;i++)
{
if(a[i]==s)
{
c=c+1;
f=1;
}
}
if(f==0)
{
cout<<"\n\t The given number is not occur";
}
else

3
{
cout<<"\n\t The given number \t"<<s<<"is occur \t"<<c<<"Times";
}
getch();
}

OUTPUT:
LINEAR SEARCHING
*********************
INPUT
******
Enter the [Link] elements: 3
Enter the elements one by one :10
20
10
Enter the number to be searched:10
OUTPUT
********
The given number 10 is occur in 2 Times.

RESULT:
Thus the above program was executed successfully and output was verified.

4
[Link] FINDING AREA OF GEOMETRIC SHAPESDate:
USING FUNCTION OVERLOADING
AIM:
To write a c++ program find the area of geometric shapes using function overloading.

ALGORITHM:
Step1: Start the program.
Step2: Declare the variable s,l,b,r,bs,ht.
Step3: Get the input value s,l,b,r,bs,ht.
Step4: Process the input values.
Step5: Get the values.
Step6: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
int area(int);
int area(int,int);
float area(float);
int area(int s)
{
return(s*s);
}
int area(int l,int b)
{
return(l*b);
}
float area(float r)
{
return(3.14*r*r);
}
float area(float bs,float ht)
{
return((bs*ht)/2);
}
void main()
{
int s,l,b;
float r,bs,ht;

5
clrscr();
cout<<"\n\t FINDING AREA OF GEOMETRIC SHAPES USING FUNCTION
OVERLOADING";
cout<<"\n\t *****************************************************";
cout<<”\n\t INPUT”;
cout<<”\n\t ******”;
cout<<"\n Enter the side of a square:";
cin>>s;
cout<<"\n Enter the length and breadth of rectangle:";
cin>>l>>b;
cout<<"\n Enter the radius of circle:";
cin>>r;
cout<<"\n Enter the base and height of triangle:";
cin>>bs>>ht;
cout<<”\n\t OUTPUT”;
cout<<”\n\t ********”;
cout<<"\n\t Area Of Geometric Shapes";
cout<<"\n\t **********************";
cout<<"\n Area of Square is:"<<area(s);
cout<<"\n Area of Rectangle is:"<<area(l,b);
cout<<"\n Area of Circle is:"<<area(r);
cout<<"\n Area of Triangle is:"<<area(bs,ht);
getch();
}

OUTPUT:
FINDING AREA OF GEOMETRIC SHAPES USING FUNCTION OVERLOADING
*************************************************************************
INPUT
******
Enter the side of a square: 2
Enter the length and breadth of rectangle: 3 4
Enter the radius of circle: 2.5
Enter the side of triangle: 2 3
OUTPUT

6
********
Area of square is: 4
Area of rectangle is: 12
Area of circle is: 19.625
Area of triangle is: 2

RESULT:
Thus the above program was executed successfully and output was verified.

[Link] BIGGEST AMONG THREE NUMBERS USING Date:


NORMAL & INLINE FUNCTION
AIM:
To write a c++ program to find the biggest among three numbers using normal & inline function.

ALGORITHM:
Step1: Start the program.
Step2: Declare the class name.
Step3: Declare the member function and inline function in inside the class.
Step4: We can define the member function using class name and scope
resolution operator in outside the class.
Step5: We can define the inline function without class name and scope
resolution operators.
Step6: We can create object for the class using object only we can access the
member function.
Step7: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class big
{

7
int x,y,z;
int a,b,c;
public:
void getdata();
void normalfunction(int,int,int);
inline int usinginline(int,int,int);
};
void big::getdata()
{
x=a;
y=b;
z=c;
}
void big::normalfunction(int x,int y,int z)
{
if((x>y)&&(x>z))
{
cout<<"\n\t"<<x<<"Is Biggest\n";
}
else if(y>x&&y>z)
{
cout<<"\n\t"<<y<<"Is Biggest\n";
}
else
{
cout<<"\n\t"<<z<<"Is Biggest\n";
}
}
inline int usinginline(int a,int b,int c)
{
a=a>b?a:b;
a=a>c?a:c;
return(a);
}
void main()
{
clrscr();
big g;
int a,b,c,n1;
cout<<"\n\t BIGGEST AMONG THREE NUMBERS USING INLINE FUNCTION";
cout<<"\n\t ***********************************************************";
read:
cout<<"\n\t INPUT";
cout<<"\n\t ******";
cout<<"\n\t Enter the number:";
cin>>a;

8
cout<<"\n\t Enter the number:";
cin>>b;
cout<<"\n\t Enter the number:";
cin>>c;
cout<<"\n\t OUTPUT";
cout<<"\n\t *********";
if(a==b&&b==c&&c==a)
{
cout<<"\n\t The Numbers are equal\n";
}
else
{
[Link]();
cout<<"\n\t Using Normal Function";
cout<<"\n\t ********************";
[Link](a,b,c);
cout<<"\n\t Using Inline Function";
cout<<"\n\t *********************";
cout<<"\n\t"<<usinginline(a,b,c)<<"Is Biggest\n";
}
cout<<"\n\t Do You Want To Continue(1/0):";
cin>>n1;
if(n1==1)
{
goto read;
}
else
{
cout<<"\n\t The Program End";
}
getch();
}

OUTPUT:
BIGGEST AMONG THREE NUMBER USING INLINE FUNCTION
************************************************************
INPUT
******
Enter the number: 5
Enter the number: 9

9
Enter the number: 6
USING NORMAL FUNCTION
***************************
9 Is Biggest
USING INLINE FUNCTION
*************************
9 Is Biggest
Do you want to continue(1/0):1
INPUT
******
Enter the number: 3
Enter the number: 3
Enter the number: 3
The numbers are equal
Do you want to continue(1/0):0
The program End.

10
RESULT:
Thus the above program was executed successfully and output was verified.

[Link]:5STUDENT MARK DETAILS USING Date:


CONSTRUCTOR
AIM:
To write a C++ program to find student mark details using constructor.

ALGORITHM:
Step1: Start the program.
Step2: Declare the class name.
Step3: We can create default constructor by using class name and whithout
pass any arguments.
Step4: We can declare the parameterised constructor by using class name and
pass the arguments.
Step5: Constructor is using the same class name.
Step6: We can create the object when automatically is called.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class student
{
int rno,mark1,mark2,mark3;
float total,avg;
char *name;
public:
student();
student(char *n,int,int,int,int);
student(student&s);
void display();

11
};
student::student()
{
name="Abi Sankar";
rno=45;
mark1=100;
mark2=100;
mark3=100;
total=mark1+mark2+mark3;
avg=total/3;
}
student::student(char *n,int rno,int m1,int m2,int m3)
{
name=n;
rno=rno;
mark1=m1;
mark2=m2;
mark3=m3;
total=mark1+mark2+mark3;
avg=total/3;
}
student::student(student&s)
{
name=[Link];
rno=[Link];
mark1=s.mark1;
mark2=s.mark2;
mark3=s.mark3;
total=mark1+mark2+mark3;
avg=total/3;
}
void student::display()
{
cout<<"\n\t Name:"<<name;
cout<<"\n\t Roll no:"<<rno;
cout<<"\n\t Mark1:"<<mark1;
cout<<"\n\t Mark2:"<<mark2;
cout<<"\n\t Mark3:"<<mark3;
cout<<"\n\t Total:"<<total;
cout<<"\n\t Average:"<<avg<<"\n";
}
void main()
{
clrscr();
char *n;
int m1,m2,m3,r;

12
student s1;
cout<<"\n\t STUDENT MARK STATEMENT USING CONSTUCTOR";
cout<<"\n\t ************************************************";
cout<<"\n\t Default Constructor";
cout<<"\n\t ****************";
[Link]();
cout<<"\n\t INPUT";
cout<<"\n\t ******";
cout<<"\n\t Enter the name:";
cin>>n;
cout<<"\n\t Enter the Roll No:";
cin>>r;
cout<<"\n\t Enter the C++:";
cin>>m1;
cout<<"\n\t Enter the CG:";
cin>>m2;
cout<<"\n\t Enter the MATHS:";
cin>>m3;
student s2(n,r,m1,m2,m3);
student s3(s2);
cout<<"\n\t OUTPUT";
cout<<"\n\t ********";
cout<<"\n\t Constructor Using Arguments";
cout<<"\n\t *************************";
[Link]();
cout<<"\n\t Copy Constructor";
cout<<"\n\t ***************";
[Link]();
getch();
}
OUTPUT:
STUDENT MARK STATEMENT USING CONSTRUCTOR
****************************************************
Default Constructor
*****************
Name: Abi Sankar
Roll No: 45
Mark1: 100
Mark2: 100

13
Mark3: 100
Total:300
Average:100
INPUT
******
Enter the name: Krishna
Enter the Roll No: 33
Enter the C++: 100
Enter the CG: 100
Enter the Maths:100
OUTPUT
********

CONSTRUCTOR USING ARGUMENTS


************************************
Name: Krishna
Roll No: 33
C++: 100
CG: 100
Maths: 100
Total: 300
Average: 100
COPY CONSTRUCTOR
**********************
Name: Krishna
Roll No: 33
Mark1: 100

14
Mark2: 100
Mark3:100
Total: 300
Average: 100

RESULT:
Thus the above program was executed successfully and output was verified.

[Link]: 6 BOOK DETAILS USING FRIEND FUNCTION Date:


AIM:
To write a C++ program to find books details using friend function.

ALGORITHM:
Step1: Start the program.
Step2: Declare the class name.

15
Step3: Declare the function name in inside.
Step4: Declare the friend function using the keyword friend in inside the
Class pass the class name and object in the arguments.
Step5: We can define the friend function without using the keyword friend,
classname & scope resolution operation.
Step6: Outside the class we can access not using friend keyword.
Step7: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class book
{
int ano,pages;
float price;
char bname[10],aname[20],publish[20];
public:
void getdata(void);
friend void showdata(book b);
};
void book::getdata()
{
cout<<"\n\t INPUT";
cout<<"\n\t *****";
cout<<"\n\t Enter the Access No:";
cin>>ano;
cout<<"\n\t Enter the Book Name:";
cin>>bname;
cout<<"\n\t Enter the Author Name:";
cin>>aname;
cout<<"\n\t Enter the Publisher Name:";
cin>>publish;
cout<<"\n\t Enter the No. of Pages:";
cin>>pages;
cout<<"\n\t Enter the Price:";
cin>>price;
}
void showdata (book b)
{
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";

16
cout<<"\n\t Access No:"<<[Link];
cout<<"\n\t Book Name:"<<[Link];
cout<<"\n\t Author Name:"<<[Link];
cout<<"\n\t Publisher Name:"<<[Link];
cout<<"\n\t No. of Pages:"<<[Link];
cout<<"\n\t Price:"<<[Link];
}
void main()
{
clrscr();
book b;
cout<<"\n\t BOOK DETAILS USING FRIEND FUNCTION";
cout<<"\n\t ****************************************";
[Link]();
showdata(b);
getch();
}

OUTPUT:
BOOK DETAILS USING FRIEND FUNCTION
*****************************************
INPUT
******
Enter the Access No: 450
Enter the Book Name: C++
Enter the Author Name: BJARNE
Enter the Publisher Name: STROSTRUP
Enter the [Link] Pages: 500
Enter the Price: 300
OUTPUT
********
Access No: 450
Book Name: C++

17
Author Name: BJARNE
Publisher Nmae: SRTOSTRUP
[Link] Pages:
Price:300

RESULT:
Thus the above program was executed successfully and output was verified.

[Link]:7CONSTRUCTOR OVERLOADING Date:


AIM:
To write a C++ program calculate to volume of cube, sphere, cylinder using constructor.

ALGORITHM:
Step1: Start the program.
Step2: Get the class constructor.
Step3: Constructor is same as class name.
Step4: To check the volume of cube using formula.
Step5: To check the volume of sphere using formula.

18
Step6: To check the volume of cylinder using formula.
Step7: Display the result of volume of cube, sphere, cylinder.
Step8: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class constr
{
int x,a;
float y,r,h,z;
public:
constr();
constr(int a,int x);
constr(float r,float y);
constr(float z,float r,float h);
};
constr::constr()
{
cout<<"\t\n Default Constructor Called";
}
constr::constr(int a,int x)
{
x=a*a*a;
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";
cout<<"\n Volume of Cube:"<<x;
}
constr::constr(float r,float y)
{
y=3.14*r*r;
cout<<"\n\t OUTPUT";
cout<<"\n\t ********";
cout<<"\n Volume of Sphere"<<y;
}
constr::constr(float z,float r,float h)
{
z=3.14*r*r*h;
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";
cout<<"\n Volume of Cylinder"<<z;
}
void main()
{

19
clrscr();
int a,x;
float z,y,r,h;
cout<<"\n\t VOLUME OF CUBE,SPHERE,CYLINDER USING CONSTRUCTOR
OVERLOADING";
cout<<"\n\t *********************************************************";
cout<<"\n\t DEFAULT CONSTRUCTOR";
cout<<"\n\t ************************";
constr c;
cout<<"\n\t\t CUBE";
cout<<"\n\t\t *****";
cout<<"\n\t INPUT";
cout<<"\n\t ******";
cout<<"\n Enter the Side:";
cin>>a;
constr(a,x);
cout<<"\n\t\t SPHERE";
cout<<"\n\t\t *******";
cout<<"\n\t INPUT";
cout<<"\n\t ******";
cout<<"\n Enter the Radius:";
cin>>r;
constr(r,y);
cout<<"\n\t\t CYLINDER";
cout<<"\n\t\t *********";
cout<<"\n Enter the Radius:";
cin>>r;
cout<<"\n Enter the Height:";
cin>>h;
constr(z,r,h);
getch();
}

OUTPUT:
CONSTRUCTOR OVERLOADING
*******************************
DEFAULT CONSTRUCTOR
*************************
Default constructor called

20
CUBE
*****
INPUT
*****
Enter the side: 3
OUTPUT
********
Volume of cube: 27
SPHERE
********
INPUT
******
Enter the radius: 2
OUTPUT
********
Volume of sphere: 25.12
CYLINDER
**********
INPUT
******
Enter the radius: 2
Enter the Height: 3
OUTPUT
********
Volume of cylinder:37.68

RESULT:

21
Thus the above program was executed successfully and output was verified.

[Link] STATIC DATA AND STATIC Date:


MEMBER FUNCTION
AIM:
To write a C++ program to perform the static data and static member function.

ALGORITHM:
Step1:Start the program.
Step2: Declare the class name “box”.
Step3: Inside the private we have to declare static int count.
Step4: Inside the public box count() using static().
Step5: The static int box count() denote function and perform object count.
Step6: Finally the function call inside the count=box::box count().
Step7: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class box
{
public:
static int count;
static int length;
static int breadth;
static int height;
box()
{
cout<<"\n\t CREATE THE OBJECTS";
cout<<"\n\t ******************";
cout<<"\n\t INPUT";
cout<<"\n\t *****";
cout<<"\n The value of the Length is:"<<length<<endl;
cout<<"\n The value of the Breadth is:"<<breadth<<endl;
cout<<"\n The value of the Height is:"<<height<<endl;
count++;
}static int boxcount()
{

22
return count;
}
};
int box::length=10;
int box::breadth=20;
int box::height=30;
int box::count;
void main()
{
clrscr();
box b,b1;
int count;
cout<<"\n\t STATIC DATA AND MEMBER FUNCTION";
cout<<"\n\t *************************************";
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";
count=box::boxcount();
cout<<"\n Count of Objects:"<<count;
getch();
}

OUTPUT:
CREATE THE OBJECTS
***********************
INPUT
******
The value of the Length is:10
The value of the Breadth is: 20
The value of the Height is: 30

CREATE THE OBJECTS


**********************
INPUT
******
The value of the Length is: 10
The value of the Breadth is: 20

23
The value of the Height is: 30
STATIC DATA AND STATIC MEMBER FUNCTION
***********************************************
OUTPUT
********
Count of the objects:2

RESULT:
Thus the above program was executed successfully and output was verified.

[Link] OVERLOADING THE WAY OF Date:


UNARY OPERATOR
AIM:
To write a C++ program to overloading the unary operator.

ALGORITHM:
Step1: Start the program.
Step2: Declare the variable x,y,z.
Step3: Get the input values for x,y,z.
Step4: To display values.
Step5: Use the minus operator and display the U operator.
Step6: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class unary
{
int x,y,z;

24
public:
void getdata();
void operator--();
void operator++();
void display1();
void display2();
};
void unary::getdata()
{
cout<<"\n\t INPUT";
cout<<"\n\t *****";
cout<<"\n\t Enter the x,y and z values:";
cin>>x>>y>>z;
}
void unary::operator--()
{
x=--x;
y=--y;
z=--z;
}
void unary::operator++()
{
x=++x;
y=++y;
z=++z;
}
void unary::display1()

25
{
cout<<"\n\t x:"<<x<<endl;
cout<<"\n\t y:"<<y<<endl;
cout<<"\n\t z:"<<z<<endl;
}
void unary::display2()
{
cout<<"\n\t x:"<<++x<<endl;
cout<<"\n\t y:"<<++y<<endl;
cout<<"\n\t z:"<<++z<<endl;
}
void main()
{
int x,y,z;
clrscr();
unary u;
cout<<"\n\t UNARY OPERATOR FOR X, Y, AND Z";
cout<<"\n\t ************************************";
[Link]();
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";
cout<<"\n Unary for Negative value \t";
cout<<"\n ******************** \t";
[Link]--();
u.display1();
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";

26
cout<<"\n Unary for Positive value \t";
cout<<"\n ******************** \t";
[Link]++();
u.display2();
getch();
}

OUTPUT:
UNARY OPERATOR FOR X,Y & Z
********************************
INPUT
******
Enter the x, y & z values: 3 4 5
OUTPUT
********
UNARY FOR NEGATIVE VALUE
******************************
x:2
y:3
z:4
OUTPUT
********
UNARY FOR POSITIVE VALUE
*****************************
x:4
y:5
z:6

27
RESULT:
Thus the above program was executed successfully and output was verified.

[Link] BINARY OPERATOR OVERLOADING Date:


AIM:
To write a C++ program for overloading the binary + & - operators.

ALGORITHM:
Step1: Start the program.
Step2: Declare the class.
Step3: Declare variables and its member function.
Step4: Using the function get value() to get the two numbers.
Step5: Define the function operator+() and operator-() to add and subtract
twocomplex numbers.
Step6: Define the display function.
Step7: Declare the class objects o1,o2,result,result1.
Step8: Call the function get value using o1 and o2.
Step9: Calculate the value for the object result by calling the function
operator+ and operator-.
Step10: Call the display function using o1,o2 and result,result1.
Step11: Return the values.
Step12: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class complex
{
int a,b;
public:

28
void getdata()
{
cout<<"\n Enter the value of Complex Numbers a,b:";
cin>>a>>b;
}
complex operator+(complex ob)
{
complex t;
t.a=a+ob.a;
t.b=b+ob.b;
return(t);
}
complex operator-(complex ob)
{
complex t;
t.a=a-ob.a;
t.b=b-ob.b;
return(t);
}
void display()
{
cout<<"\n"<<a<<"+"<<b<<"i"<<"\n";
}
};
void main()
{
clrscr();
complex o1,o2,result,result1;
cout<<"\n\t BINARY OPERATOR OVERLOADING";
cout<<"\n\t *********************************";
[Link]();
[Link]();
result=o1+o2;
result1=o1-o2;
cout<<"\n\t INPUT";
cout<<"\n\t *****";
[Link]();
[Link]();
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";
cout<<"\n '+' Operator Overloading";
cout<<"\n ********************";
[Link]();
cout<<"\n '-' Operator Overloading";
cout<<"\n *******************";
[Link]();

29
getch();
}

OUTPUT:
BINARY OPERATOR OVERLOADING
************************************
Enter the value of complex number a,b: 8 8
Enter the value of complex number a,b: 5 2

INPUT
******
8+8i
5+2i
OUTPUT
********
‘+’ OPERATOR OVERLOADING
******************************
13+10i
‘-‘ OPERATOR OVERLOADING
*****************************
3+6i

RESULT:
Thus the above program was executed successfully and output was verified.

30
[Link]: 11 SINGLE INHERITANCE USING BIO-DATA Date:
AIM:
To write a C++ program using the Single Inheritance Using Bio-Data.

ALGORITHM:
Step1: Start the program.
Step2: Declare a class name bio.
Step3: Declare the one base class and one derived class using a single
inheritance.
Step4: We can define the member function using derived class name object
and scope resolution operation in outside the class.
Step5: One base class using another class derived class declared.
Step6: Derived class name using the object only we can access the member
function.
Step7: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class bio
{
protected:
char name[10],fn[15],na[10],qu[15],sex[10],date[15];
int age;
public:
void getdata();
};
void bio::getdata()
{
cout<<"\n\t INPUT";
cout<<"\n\t *****";
cout<<"\n\t ENTER THE NAME =";
cin>>name;
cout<<"\n\t ENTER THE FATHER NAME =";
cin>>fn;
cout<<"\n\t ENTER THE SEX =";
cin>>sex;

31
cout<<"\n\t ENTER THE AGE =";
cin>>age;
cout<<"\n\t ENTER THE DATE OF BIRTH =";
cin>>date;
cout<<"\n\t ENTER THE NATIONALITY =";
cin>>na;
cout<<"\n\t ENTER THE QUALIFICATION =";
cin>>qu;
}
class data:public bio
{
public:
void display();
};
void data::display()
{
cout<<"\n\t OUTPUT";
cout<<"\n\t ******";
cout<<"\n\t NAME :"<<name;
cout<<"\n\t FATHER NAME :"<<fn;
cout<<"\n\t SEX :"<<sex;
cout<<"\n\t AGE :"<<age;
cout<<"\n\t DATE OF BIRTH :"<<date;
cout<<"\n\t NATIONALITY :"<<na;
cout<<"\n\t QUALIFICATION :"<<qu;
}
void main()
{
clrscr();
cout<<"\n\t SINGLE INHERITANCE USING BIODATA";
cout<<"\n\t **************************************";
data d;
[Link]();
[Link]();
getch();
}
OUTPUT:

SINGLE INHERITANCE USING BIO-DATA


***************************************
INPUT
******
ENTER THE NAME = ABISANKAR.M
ENTER THE FATHER NAME = MARISAMY.K
ENTER THE SEX = MALE
ENTER THE AGE = 19

32
ENTER THE DATE OF BIRTH = 31/12/2004
ENTER THE NATIONALITY = INDIAN
ENTER THE QUALIFICATION = BSC

OUTPUT
********
NAME : ABISANKAR.M
FATHER NAME : MARISAMY.K
SEX : MALE
AGE : 19
DATE OF BIRTH : 31/12/2004
NATIONALITY : INDIAN
QUALIFICATION : BSC

RESULT:
Thus the above program was executed successfully and output was verified.
[Link] DEMONSTRATING MULTIPLE INHERITANCE Date:
AIM:
To write a C++ program to demonstrate the use of multiple inheritance.

ALGORITHM:
Step1: Start the program.
Step2: Get the values for name,eno & basic pay.
Step3: Processing the input values.
Step4: Display the values.
Step5: Stop the program.

SOURCE CODE:

33
#include<iostream.h>
#include<conio.h>
class emp
{
public:
int eno;
char name[20];
void get()
{
cout<<"\n DEMONSTRATING THE MULTIPLE INHERITANCE";
cout<<"\n **********************************************";
cout<<"\nINPUT";
cout<<"\n*****";
cout<<"Enter the name for employee:";
cin>>name;
cout<<"Enter the employee number:";
cin>>eno;
}
};
class cal
{
public:
long int bp;
float hra,da,pf,it;
void process()
{
cout<<"Enter the basic pay for employee:";
cin>>bp;
hra=bp*0.02;
da=bp*0.01;
pf=bp*0.01;
if(bp>=50000)
it=bp*0.01;
else
it=0.00;
}
};
class netpay:publicemp,publiccal
{
public:
float gp,np;
void display()
{
gp=bp+hra+da;
np=gp-it-pf;
cout<<"\nOUTPUT";

34
cout<<"\n******";
cout<<"\n\t EMPLOYEE DETAILS";
cout<<"\n\t ------------------------------";
cout<<"\nName\tEno\tBp\tHRA\tDA\tPF\tIT\tGP\tNP\t";
cout<<"\n--------------------------------------------------------------------------------\t";
cout<<"\n"<<name<<"\t"<<eno<<"\t"<<bp<<"\t"<<hra<<"\t"<<da<<"\t"<<pf<<"\t"<<it<<"\
t"<<gp<<"\t"<<np<<"\t";
cout<<"\n--------------------------------------------------------------------------------\t";
}
};
void main()
{
clrscr();
netpay n;
[Link]();
[Link]();
[Link]();
getch();
}

OUTPUT:
DEMONSTRATING THE MULTIPLE INHERITANCE
*************************************************
INPUT
******
Enter the Employee Name: ABI
Enter the Employee Number: 264
Enter the Basic pay for Employee:1000
OUTPUT
********

EMPLOYEE DETAILS
-------------------------------
-----------------------------------------------------------------------------------
Name Eno BP HRA DA PF IT GP NP
ABI 264 1000 20 10 10 0 1030 1020
-----------------------------------------------------------------------------------

35
RESULT:
Thus the above program was executed successfully and output was verified.
[Link] DEMONSTRATE HIERARCHICAL Date:
INHERITANCE
AIM:
To write a C++ program to demonstrate the use of hierarchical inheritance.

ALGORITHM:
Step1: Start the program.
Step2: Declare the variables.
Step3: Get the input values.
Step4: Print the input values.
Step5: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
class A
{
public:
int a;
void getch()
{
cout<<"\n INPUT";
cout<<"\n *****";
cout<<"\n Enter the value for a:";
cin>>a;
}
};
class square:public A
{
public:

36
int sq;
void cals()
{
sq=a*a;
cout<<"\n OUTPUT";
cout<<"\n ******";
cout<<"\n Square of a:"<<sq;
}
};
class cube:public A
{
public:
int cu;
void calc()
{
cu=a*a*a;
cout<<"\n OUTPUT";
cout<<"\n ******";
cout<<"\n Cube of a:"<<cu;
}
};
void main()
{
clrscr();
cout<<"\n\t DEMONSTRATING HIERARCHIAL INHERITANCE";
cout<<"\n\t **********************************************";
cout<<"\n\t SQUARE & CUBE";
cout<<"\n\t ****************";
square s;
[Link]();
[Link]();
cube c;
[Link]();
[Link]();
getch();
}

OUTPUT:

DEMONSTRATING HIERARCHICAL INHERITANCE

*************************************************
SQUARE & CUBE
*****************
INPUT
******

37
Enter the value for a: 4
OUTPUT
********
Square of a:16
INPUT
******
Enter the value for a: 2
OUTPUT
********
Cube of a: 8

RESULT:
Thus the above program was executed successfully and output was verified.
[Link] DEMONSTRATING THE MULTILEVEL Date:
INHERITANCE
AIM:
To write a C++ program for multilevel Inheritance.

ALGORITHM:
Step1: Start the program.
Step2: Get the input values for rno,name,m1,m2,m3.
Step3: Processing the input values.
Step4: Display the values.
Step5: Stop the program.

SOURCE CODE:
/#include<iostream.h>
#include<conio.h>
class A
{
public:
int rollno;
char name[20];
void get()
{
cout<<"\n Enter the name and roll number of student: \t";
cin>>name>>rollno;
}
void display()

38
{
cout<<name<<"\t"<<rollno<<"\t";
}
};
class B:public A
{
public:
int m1,m2,m3;
void getm()
{
cout<<"\n Enter the marks \t:";
cin>>m1>>m2>>m3;
}
void dispm()
{
cout<<m1<<"\t"<<m2<<"\t"<<m3<<"\t";
}
};
class C:public B
{
public:
int tot;
float avg;
void process()
{
tot=m1+m2+m3;
avg=tot/3;
}
void dispt()
{
cout<<tot<<"\t"<<avg<<"\t";
}
};
void main()
{
C c;
clrscr();
cout<<”\n\t DEMONSTRATING THE MULTILEVEL INHERITANCE”;
cout<<”\n\t ************************************************”;
cout<<”\n INPUT”;
cout<<”\n ******”;
[Link]();
[Link]();
[Link]();
cout<<”\n OUTPUT”;
cout<<”\n ********”;

39
cout<<"\n\t STUDENT MARK LIST";
cout<<"\n\t ********************";
cout<<"*************************************************** \n";
cout<<"Name \tRollno \tM1 \tM2 \tM3 \tTotal \tAverage: \n";
cout<<"*************************************************** \n";
[Link]();
[Link]();
[Link]();
getch();
}

OUTPUT:
DEMONSTRATING THE MULTILEVEL INHERITANCE
**************************************************
INPUT
******
Enter the name and Roll number of student: Abi
264
Enter the marks: 90 80 90

OUTPUT
*******
STUDENT MARK LIST
*********************
***************************************************************
Name Roll No M1 M2 M3 Total Average
***************************************************************
Abi 264 90 80 90 260 86

RESULT:
Thus the above program was executed successfully and output was verified.
[Link] ILLUSTRATING CLASS TEMPLATES Date:
AIM:

40
To write a c++ program to use the Class Templates.

ALGORITHM:
Step1: Start the program.
Step2: Declare the values.
Step3: Assigning the values.
Step4: Get the input value for.
Step5: Function are called in this place and process the values.
Step6: Print the output value.
Step7: Stop the program.

SOURCE CODE:
#include<iostream.h>
#include<conio.h>
template <class T>
class calculator
{
Private:
T num1,num2;
Public:
Calculator(T n1,T n2)
{
num1=n1;
num2=n2;
}
void displayresult()
{
cout<<”Numbers are:”<<num1<<”and”<<num2<<” “<<endl;
cout<<”Addition:”<<add()<<endl;

41
cout<<”Subtraction:”<<subtract()<<endl;
cout<<”Product:”<<multiply()<<endl;
cout<<”Division:”<<division()<<endl;
}
T add(){return num1+num2;}
T subtract(){return num1-num2;}
T multiply(){return num1*num2;}
T division(){return num1/num2;}
};
void main()
{
int a,b;
float c,d;

cout<<”\n\t ILLUSTRATING CLASS TEMPLATES”;

cout<<”\n\t ********************************”;
cout<<”\nINPUT”;
cout<<”\n******”;
cout<<”Enter any two integer numbers”;
cin>>a>>b;
cout<<”Enter any two real numbers”;
cin>>c>>d;
calculator<int>intcalc(a,b);
calculator<float>floatcalc(c,d);
cout<<”\nOUTPUT”;
cout<<”\n********”;
cout<<”Integer Results:”<<endl;
[Link]();

42
cout<<endl<<”Float results:”<<endl;
[Link]();
getch();
}

OUTPUT:
ILLUSTRATING CLASS TEMPLATES
***********************************
INPUT
******
Enter any two integer numbers: 3 3
Enter any two real numbers: 3.1 3.1

OUTPUT
********
Integer results:
Numbers are: 3 and 3
Addition: 6
Subtraction: 0
Product: 9
Division: 1
Float results:
Numbers are: 3.1 and 3.1
Addition: 6.2
Subtraction: 0.0
Product: 9.61
Division: 1

43
RESULT:
Thus the above program was executed successfully and output was verified.
[Link] FUNCTION TEMPLATE USING LARGEST Date:
AMONG TWO NUMBERS
AIM:
To write a C++ Program to use the Function Template.

ALGORITHM:
Step1: Start the Program.
Step2: Declare the Value.
Step3: Gets the Input Value.
Step4: Compare the values then largest values is printed.
Step5: Display the Output.
Step6: Stop the Program.
SOURCE CODE:
#include<stdio.h>
#include<conio.h>
#include <iostream.h>
template <class T>
T Large(T n1, T n2)
{
return (n1 > n2) ? n1 : n2;
}
int main()
{
int i1, i2;
float f1, f2;
char c1, c2;
clrscr();
cout<<"\n\tFUNCTION TEMPLATE USING LARGEST AMONG TWO NUMBERS";
cout<<"\n\t*************************************************************";
cout<<"\n\tINPUT";
cout<<"\n\t*****";
cout<< "\nEnter two integers:\n";
cin>> i1 >> i2;
cout<< "\nEnter two floating-point numbers:\n";
cin>> f1 >> f2;
cout<< "\nEnter two characters:\n";
cin>> c1 >> c2;
cout<<"\n\tOUTPUT";
cout<<"\n\t******\n";

44
cout<<Large(i1, i2) <<" is larger." <<endl;
cout<<Large(f1, f2) <<" is larger." <<endl;
cout<<Large(c1, c2) << " has larger ASCII value.";
getch();
return 0;
}
OUTPUT:
FUNCTION TEMPLATE USING LARGEST AMONG TWO NUMBERS
***************************************************************
INPUT
******
Enter two integers:
34 98
Enter two floating-point numbers:
3.4 6.7
Enter the two characters:
wu
OUTPUT
********
98 is larger
6.7 is larger
w has larger ASCII value

RESULT:
Thus the above program was executed successfully and output was verified.

45

You might also like