0% found this document useful (0 votes)
3 views24 pages

Corrected Program 2024

The document contains multiple C++ source code examples demonstrating various programming concepts including function overloading with default arguments, student mark list using classes, passing objects to functions, friend functions, constructors and destructors, unary and binary operator overloading, single and multilevel inheritance. Each section provides a brief description of the functionality and includes the corresponding source code. The examples illustrate the use of classes, methods, and operator overloading in C++.

Uploaded by

dorayakey.65
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)
3 views24 pages

Corrected Program 2024

The document contains multiple C++ source code examples demonstrating various programming concepts including function overloading with default arguments, student mark list using classes, passing objects to functions, friend functions, constructors and destructors, unary and binary operator overloading, single and multilevel inheritance. Each section provides a brief description of the functionality and includes the corresponding source code. The examples illustrate the use of classes, methods, and operator overloading in C++.

Uploaded by

dorayakey.65
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
You are on page 1/ 24

FUNCTION OVERLOADING WITH DEFAULT ARGUMENTS

AND INLINE FUNCTION

SOURCE CODE

#include<iostream.h>
#include<conio.h>
class shape
{
public:
inline void area(int i)
{
cout<<"\n\t Area of square : "<<(i* i);
}
void area(double,double);
void area(int,int);
};
void shape :: area(double r,double pi=3.14)
{
cout<<"\n\t Area of circle : "<<(pi*r*r) ;
}
void shape :: area(int l, int b)
{
cout<<"\n\t Area of rectangle : "<<(l*b);
}
void main ( )
{
shape s;
int side,length,breadth;
double radius;
clrscr();
cout<<"\n\tFUNCTION OVERLOADING WITH DEFAULT ARGUMENTS
AND INLINE FUNCTION";
cout<<"\n\t---------------------------------------------------------------";
cout<<"\n\nArea of Square";
cout<<"\n-----------------";
cout<<"\nEnter the side :";
cin>>side ;
s.area(side);
cout<<"\n\nArea of Circle";
cout<<"\n---------------";
cout<<"\nEnter the radius : ";
cin>>radius;
s.area(radius);
cout<<"\n\nArea of Rectangle";
cout<<"\n-------------------";
cout<<"\nEnter the length : ";
cin>>length ;
cout<<"\nEnter the breadth : " ;
cin>>breadth ;
s.area(length,breadth);
getch( );
}
OUTPUT
STUDENT MARKLIST USING CLASS AND OBJECT

SOURCE CODE

#include<iostream.h>
#include<conio.h>
class student
{
public:
int Regno;
char strname[30];
float m1,m2,m3,m4,m5,Total,Avg;
void in();
void calculate();
void out();
};
void student::in()
{
cout<<"\nEnter the Regno:";
cin>>Regno;
cout<<"\nEnter the Name:";
cin>>strname;
cout<<"\nEnter the Tamil Mark:";
cin>>m1;
cout<<"\nEnter the English Mark:";
cin>>m2;
cout<<"\nEnter the Maths Mark:";
cin>>m3;
cout<<"\nEnter the Science Mark:";
cin>>m4;
cout<<"\nEnter the Social Mark:";
cin>>m5;
};
void student::calculate()
{
Total=m1+m2+m3+m4+m5;
Avg=Total/5;
};
void student::out()
{
cout<<"\n\n\t\tSTUDENT MARK DETAILS";
cout<<"\n\t\t--------------------";
cout<<"\nRegno :" <<Regno;
cout<<"\nName :" <<strname;
cout<<"\nTamil :" <<m1;
cout<<"\nEnglish :" <<m2;
cout<<"\nMaths :" <<m3;
cout<<"\nScience :" <<m4;
cout<<"\nSocial :" <<m5;
cout<<"\nTotal :" <<Total;
cout<<"\nAverage :" <<Avg;
};
void main()
{
student x;
clrscr();
cout<<"\n\t\tSTUDENT MARK DETAILS USING CLASS AND OBJECT";
cout<<"\n\t\t-------------------------------------------";
x.in();
x.calculate();
x.out();
getch();
}
OUTPUT
PASSING AN OBJECT TO FUNCTION

SOURCE CODE

#include<iostream.h>
#include<conio.h>
class pass
{
public:
int a,b;
void get()
{
cin>>a;
}
void multi(pass o,pass p)
{
int c=o.a*p.a;
cout<<"\n\n"<<o.a<<" * "<<p.a<<" = "<<c;
}
};

void main()
{
pass f,s,r;
clrscr();
cout<<"\n\t**PASSING OBJECTS TO FUNCTION**";
cout<<"\n\t---------------------------------";
cout<<"\n\nEnter the first object value: ";
f.get();
cout<<"\nEnter the second object value: ";
s.get();
cout<<"\n\nMultiplication of values in two objects";
cout<<"\n-----------------------------------------";
r.multi(f,s);
getch();
}
OUTPUT
FRIEND FUNCTION
SOURCE CODE

#include<iostream.h>
#include<conio.h>
class second;
class first
{
int x;
public:
void get()
{
cout<<"\nEnter the first object value: ";
cin>>x;
}
friend void large (first,second);
};
class second
{
int y;
public:
void get()
{
cout<<"\nEnter the second object value :";
cin>>y;
}
friend void large (first,second);
};
void large(first m,second n)
{
int x,y;
if(m.x<n.y)
{
cout<<"\n\nMinimum number is :"<<m.x;
}
else
{
cout<<"\n\nMinimum number is :"<<n.y;
}
};
void main()
{
first f;
second s;
clrscr();
cout<<"\n\t**FRIEND FUNCTION**";
cout<<"\n\t ------------------";
cout<<"\n\n *Minimum of two objects*";
cout<<"\n----------------------------";
f.get();
s.get();
large(f,s);
getch();
}
OUTPUT
CONSTRUCTOR AND DESTRUCTOR

SOURCE CODE
#include<iostream.h>
#include<conio.h>
#include<string.h>
class cons
{
public:
int base,height;
cons(int x,int y)
{
cout<<"\n\n\nCreate and Initialize an Object";
cout<<"\n-------------------------------";
base=x;
height=y;
}
void print()
{
cout<<"\n\nBase : "<<base;
cout<<"\n\nHeight : "<<height;
cout << "\n\n\t\tArea of triangle : "<<(0.5*base*height);
}
~cons()
{
cout << "\n\n\n*** Object destroyed successfully ***";
}
};
void main()
{
clrscr();
int b,h;
cout << "\n\t\t\t**Constructor and Destructor**";
cout << "\n\t\t\t------------------------------";
cout << "\n\nArea of triangle";
cout << "\n\-----------------";
cout <<"\n\n Enter the Base:";
cin >> b;
cout <<"\n Enter the height :";
cin >> h;
cons obj(b,h);
obj.print();
getch();
}
OUTPUT
UNARY OPERATOR OVERLOADING

SOURCE CODE
#include<iostream.h>
#include<conio.h>
class sign
{
int x,y,z;
public:
void get();
void display();
void operator-();
};
void sign::get()
{
cout<<"\n\nEnter the X value :";
cin>>x;
cout<<"\nEnter the Y value :";
cin>>y;
cout<<"\nEnter the Z value :";
cin>>z;
}
void sign::display()
{
cout<<"\nX= "<<x;
cout<<"\nY= "<<y;
cout<<"\nZ= "<<z;
}
void sign::operator-()
{
x=-x;
y=-y;
z=-z;
}
void main()
{
int a,b,c;
clrscr();
cout<<"\t\tUNARY OPERATOR OVERLOADING";
cout<<"\n\t\t-------------------------";
sign s;
s.get();
cout<<"\n\nBefore Negation:";
cout<<"\n------------------";
s.display();
-s;
cout<<"\n\nAfter Negation:";
cout<<"\n------------------";
s.display();
getch();
}
OUTPUT
BINARY OPERATOR OVERLOADING
SOURCE CODE
#include<iostream.h>
#include<conio.h>
class binary
{
public:
int r, i;
void get( );
binary operator +(binary);
void display( );
};
void binary::get( )
{
cout<<"\n Enter the Real Part: ";
cin>>r;
cout<<"\n Enter the Imaginary Part: ";
cin>>i;
}
binary binary::operator +(binary d)
{
binary c;
c.r=r+d.r;
c.i=i+d.i;
return c;
}
void binary::display( )
{
cout<<"\t"<<r<<"+"<<i<<"i";
}
void main( )
{
binary b1, b2, b3;
clrscr( );
cout<<"\n\t\t BINARY OPERATOR OVERLOADING";
cout<<"\n\t\t ***************************";
cout<<"\n\nEnter the first complex number ";
cout<<"\n-------------------------------";
b1.get( );
cout<<"\n\nEnter the second complex number ";
cout<<"\n--------------------------------";
b2.get( );
cout<<"\n\nSum of two complex numbers ";
cout<<"\n--------------------------------";
b3=b1+b2;
cout<<"\n\n First complex number : ";
b1.display( );
cout<<"\n Second complex number : ";
b2.display( );
cout<<"\n\t\t\t----------------";
cout<<"\n Sum : ";
b3.display( );
cout<<"\n\t\t\t----------------";
getch( );
}
OUTPUT
SINGLE INHERITANCE

SOURCE CODE
#include<iostream.h>
#include<conio.h>
class first
{
public:
int a;
void get()
{
cout<<"\n\nEnter a value:";
cin>>a;
}
};
class second:public first
{
public:
void check()
{
if(a%2==0)
cout<<"\n\n"<<a<<" is an even number";
else
cout<<"\n\n"<<a<<" is an odd number";
}
};
void main()
{
clrscr();
second obj;
cout<<"\n\t\tSINGLE INHERITANCE";
cout<<"\n\t\t-------------------";
cout<<"\n\nTo check whether the given number is odd or even:";
cout<<"\n-------------------------------------------------";
obj.get();
obj.check();
getch();
}

OUTPUT
MULTILEVEL INHERITANCE

SOURCE CODE

#include<iostream.h>
#include<conio.h>
class book
{
public:
int bookid;
char bname[30];
float price;
void input( );
};
void book::input( )
{
cout<<"\n Enter the Book ID: ";
cin>>bookid;
cout<<"\n Enter the Book Name: ";
cin.ignore();
cin.getline(bname,30);
cout<<"\n Enter the Price: ";
cin>>price;
}
class purchase:public book
{
public:
int quantity;
void inputquantity( );
};
void purchase::inputquantity( )
{
cout<<"\n Enter the Quantity Purchased:";
cin>>quantity;
}
class detail:public purchase
{
public:
void display( );
};
void detail::display( )
{
cout<<"\n\t\t BOOK DETAILS";
cout<<"\n\t\t ------------";
cout<<"\n\n Book ID :"<<bookid;
cout<<"\n\n Book Name :"<<bname;
cout<<"\n\n Unit Price :"<<price;
cout<<"\n\n Quantity :"<<quantity;
cout<<"\n\n Total Amount :"<<(price*quantity);
}
void main( )
{
detail ob;
clrscr( );
cout<<"\n\t\tBOOK DETAILS USING MULTILEVEL INHERITANCE";
cout<<"\n\t\t-----------------------------------------";
ob.input( );
ob.inputquantity( );
ob.display( );
getch( );
}
OUTPUT

You might also like