Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
4 views49 pages

Record 1

The document contains multiple C++ source code examples demonstrating various programming concepts such as function overloading, classes, constructors, operator overloading, inheritance (single, multilevel, multiple, hierarchical, hybrid), virtual functions, and file handling. Each section includes a brief explanation of the concept followed by the corresponding code and expected output. The examples illustrate practical applications of these concepts in a clear and structured manner.

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 PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views49 pages

Record 1

The document contains multiple C++ source code examples demonstrating various programming concepts such as function overloading, classes, constructors, operator overloading, inheritance (single, multilevel, multiple, hierarchical, hybrid), virtual functions, and file handling. Each section includes a brief explanation of the concept followed by the corresponding code and expected output. The examples illustrate practical applications of these concepts in a clear and structured manner.

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 PDF, TXT or read online on Scribd
You are on page 1/ 49

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
FACTORIAL OF A NUMBER USING CLASS AND OBJECT

SOURCE CODE

#include<iostream.h>
#include<conio.h>
class factorial
{
public:
int n,f;
void in();
void calculate();
void out();
};
void factorial::in()
{
cout<<"\n\nEnter a number :";
cin>>n;
}
void factorial::calculate()
{
f=1;
for(int i=1;i<=n;i++)
f=f*i;
}
void factorial::out()
{
cout<<"\n\n\t\tFactorial of given number : "<<f;
}
void main()
{
factorial x;
clrscr();
cout<<"\n\t\t\tClass and Object";
cout<<"\n\t\t\t----------------";
cout<<"\n\nFactorial of a number";
cout<<"\n-----------------------";
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;
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 min (first,second);
};
class second
{
int y;
public:
void get()
{
cout<<"\nEnter the second object value :";
cin>>y;
}
friend void min (first,second);
};
void min(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();
min(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
MULTIPLE INHERITANCE
SOURCE CODE

#include<iostream.h>
#include<conio.h>
class employee
{
public:
int eid;
char name[30];
char desig[30];
void getemp()
{
cout<<"\n Enter the employee ID :";
cin>>eid;
cout<<"\n Enter the employee name :";
cin>>name;
cout<<"\n Enter the designation :";
cin>>desig;
}
};
class salary
{
public:
float bp;
float pf;
float np;
void getsal()
{
cout<<"\n Enter the Basic Pay : ";
cin>>bp;
pf=bp*0.12;
np=bp-pf;
}
};
class detail:public employee,public salary
{
public:
void display()
{
cout<<"\n\nEmployee ID : "<<eid;
cout<<"\nEmployee Name : "<<name;
cout<<"\nDesignation : "<<desig;
cout<<"\nBasic Pay : "<<bp;
cout<<"\nProvident Fund: "<<pf;
cout<<"\nNet Pay : "<<np;
}
};
void main()
{
clrscr();
cout<<"\n\t MULTIPLE INHERITANCE";
cout<<"\n\t----------------------";
cout<<"\n\nEmployee Detail:";
cout<<"\n----------------";
detail d;
d.getemp(
);
d.getsal();
d.display();
getch();
}
OUTPUT
HIERARCHICAL INHERITANCE
SOURCE CODE

#include<iostream.h>
#include<conio.h>
class cube
{
public:
int side;
void get()
{
cout<<"\n\nEnter the side:";
cin>>side;
}
};
class area:public cube
{
public:
void showarea()
{
get();
cout<<"\n\n\t\tSurface Area of cube : "<<(6*side*side);
}
};
class volume:public cube
{
public:
void showvolume()
{
get();
cout<<"\n\n\t\tVolume of cube : "<<(side*side*side);
}
};
void main()
{
clrscr();
cout<<"\n\t\tHierarchical Inheritance";
cout<<"\n\t\t-------------------------";
cout<<"\nTo Find the surface area of cube:";
cout<<"\n---------------------------------";
area a;
a.showarea();
cout<<"\n\nTo Find the volume of cube:";
cout<<"\n---------------------------";
volume v;
v.showvolume();
getch();
}
OUTPUT
HYBRID INHERITANCE
SOURCE CODE

#include<iostream.h>
#include<conio.h>
class personal
{
public:
int regno;
char name[50];
void stud()
{
cout<<"\n Enter the student ID : ";
cin>>regno;
cout<<"\n Enter the student name : ";
cin>>name;
}
};
class mark:public personal
{
public:
int m1,m2,m3;
void test()
{
cout<<"\n Enter C++ Mark : ";
cin>>m1;
cout<<"\n Enter PHP Mark : ";
cin>>m2;
cout<<"\n Enter HTML Mark : ";
cin>>m3;
}
};
class sports
{
public:
float m;
void extra()
{
cout<<"\n Enter the mark for extra curricular activity : ";
cin>>m;
}
};
class detail:public mark,public sports
{
public:
void display()
{
cout<<"\n\t Student Mark Details";
cout<<"\n\t --------------------\n";
cout<<"\n Student ID : "<<regno<<"\n";
cout<<"\n Student Name : "<<name<<"\n";
cout<<"\n C++ mark : "<<m1<<"\n";
cout<<"\n PHP mark : "<<m2<<"\n";
cout<<"\n HTML mark : "<<m3<<"\n";
cout<<"\n Extra curricular Activity: "<<m<<"\n";
cout<<"\n Total : "<<(m1+m2+m3+m)<<"\n";
}
};
void main()
{
detail obj;
clrscr();
cout<<"\n\t\t\t HYBRID INHERITANCE\n";
cout<<"\t\t\t--------------------\n";
obj.stud();
obj.test();
obj.extra();
obj.display();
getch();
}
OUTPUT
VIRTUAL FUNCTION
SOURCE CODE

#include<iostream.h>
#include<conio.h>
class shape
{
public:
virtual void getvolume()=0;
};
class cuboid:public shape
{
private:
float length;
float width;
float height;
public:
cuboid(float l,float w,float h)
{
length=l;
width=w;
height=h;
}
void getvolume()
{
double volume= length*width*height;
cout<<"\n\n\t\tVolume of cuboid :"<<volume;
}
};
void main()
{
float l,w,h;
clrscr();
cout<<"\n\t\t\tVIRTUAL FUNCTION\n";
cout<<"\n\t\t\t----------------\n";
cout<<"\nEnter the length of cuboid :";
cin>>l;
cout<<"\nEnter the width of cuboid :";
cin>>w;
cout<<"\nEnter the height of cuboid :";
cin>>h;
cuboid c(l,w,h);
c.getvolume();
getch();
}
OUTPUT
FILE HANDLING
SOURCE CODE

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ofstream of("chem.txt");
int id,n,qty,price,p,total=0;
char name[10],pname[10];
cout<<"\n\t\tCHEMICAL SHOP";
cout<<"\n\t\t-------------";
cout<<"\nEnter Number of Customer:";
cin>>n;
for(int i=1;i<=n;i++)
{
cout<<"\nCUSTOMER NUMBER"<<i;
cout<<"\nEnter Customer Name:";
cin>>name;
cout<<"\nEnter Product ID:";
cin>>id;
cout<<"\nEnter Chemical Name:";
cin>>pname;
cout<<"\nEnter Unit Price:";
cin>>price;
cout<<"\nEnter Product Quantity:";
cin>>qty;
p=price*qty;
total=total+p;
of<<name<<"\t\t"<<id<<"\t"<<pname<<"\t\t"<<price<<"\t"<<qty<<"\t\t"<<p<
<"\n";
}
cout<<"\n------ FILE UPDATED ------";
cout<<"\n";
of.close();
cout<<"\n\t\t\tFERTILIZER BILL";
cout<<"\n-------------------------------------------------------------------------------";
ifstream fi("chem.txt");
cout<<"\n\nName"<<"\t"<<"PID"<<"\t"<<"ProductName"<<"\t"<<"UnitPrice"
<<"\t"<<"Quantity"<<"\t"<<"Amount"<<"\n";
cout<<"-------------------------------------------------------------------------------\n";
for(int j=1;j<=n;j++)
{
fi>>name>>id>>pname>>price>>qty>>p;
cout<<name<<"\t"<<id<<"\t"<<pname<<"\t\t"<<price<<"\t"<<qty<<"\t"<<p<
<"\n";
}
cout<<"\n-------------------------------------------------------------------------------";
cout<<"\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tTotal:"<<total;
cout<<"\n-------------------------------------------------------------------------------";
fi.close();
getch();
}
OUTPUT
SEQUENTIAL I/O OPERATIONS ON A FILE

SOURCE CODE

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>
void main()
{
int l,i;
char s[200];
char ch;
fstream f;
clrscr();
cout<<"\n\t\t\tFILE OPERATIONS";
cout<<"\n\t\t\t---------------\n";
cout<<"\nEnter a Paragraph:";
cout<<"\n------------------\n";
cin.getline(s,200);
cout<<"\n\nParagraph in File:";
cout<<"\n--------------------\n";
cout<<s;
f.open("para.txt",ios::in|ios::out);
l=strlen(s);
for(i=0;i<l;i++)
f.put(s[i]);
f.seekg(0);
for(i=0;i<l;i++)
{
f.get(ch);
cout.put(ch);
}
getch();
}
OUTPUT
BIGGEST NUMBER USING COMMAND LINE ARGUMENTS

SOURCE CODE

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
void main(int count,char*a[])
{
int big=0;
clrscr();
cout<<"\n\t\t\tCOMMAND LINE ARGUMENTS";
cout<<"\n\t\t\t----------------------";
cout<<"\nBIGGEST NUMBER";
cout<<"\n--------------";
cout<<"\n\nThe arguments are : ";
for(int i=1;i<count;i++)
{
cout<<"\n"<<a[i];
int n=atoi(a[i]);
if(big<n)
{
big=n;
}
}
cout<<"\n\n\tBiggest number is : "<<big;
getch();
}
OUTPUT
CLASS TEMPLATE

SOURCE CODE

#include<iostream.h>
#include<conio.h>
template<class T>
class subtract
{
public:
T n1,n2;
subtract(T x,T y)
{
n1=x;
n2=y;
cout<<"\nx="<<n1;
cout<<"\ny="<<n2;
}
void minus()
{
cout<<"\n\t"<<n1<<" - "<<n2<<" = "<<(n1-n2);
}
};
void main()
{
clrscr();
cout<<"\t\t\tCLASS TEMPLATE\n";
cout<<"\t\t\t--------------\n";
int a,b;
float c,d;
double e,f;
cout<<"\n\nDifference of two Integer values\n";
cout<<"----------------------------------";
cout<<"\nEnter two integer values :";
cin>>a>>b;
subtract<int>o1(a,b);
o1.minus();
cout<<"\n\nDifference of two Float values\n";
cout<<"----------------------------------";
cout<<"\nEnter two float values :";
cin>>c>>d;
subtract<float>o2(c,d);
o2.minus();
cout<<"\n\nDifference of two Double values\n";
cout<<"---------------------------------";
cout<<"\nEnter two double values :";
cin>>e>>f;
subtract<double>o3(e,f);
o3.minus();
getch();
}
OUTPUT
FUNCTION TEMPLATE

SOURCE CODE

#include<iostream.h>
#include<conio.h>
template<class temp>
void swap(temp x,temp y)
{
temp t;
cout<<"\nBefore Swap:";
cout<<"\tA="<<x<<"\t\tB="<<y;
t=x;
x=y;
y=t;
cout<<"\n\nAfter Swap:";
cout<<"\tA="<<x<<"\t\tB="<<y;
}
void main()
{
int a,b;
float c,d;
char e,f;
clrscr();
cout<<"\t\t\tFUNCTION TEMPLATE";
cout<<"\n\t\t\t----------------";
cout<<"\n\nSwapping two integer numbers :";
cout<<"\n------------------------------";
cout<<"\nEnter two integer numbers :";
cin>>a>>b;
swap(a,b);
cout<<"\n\nSwapping two float numbers :";
cout<<"\n----------------------------";
cout<<"\nEnter two float numbers :";
cin>>c>>d;
swap(c,d);
cout<<"\n\nSwapping two characters :";
cout<<"\n--------------------------";
cout<<"\nEnter two characters :";
cin>>e>>f;
swap(e,f);
getch();
}
OUTPUT
EXCEPTION HANDLING

SOURCE CODE

#include<iostream>
using namespace std;
int main()
{
int numerator,denominator,divide;
cout<<"\n\t\t\t EXCEPTION HANDLING";
cout<<"\n\t\t\t ------------------";
cout<<"\n\n Enter the numerator :";
cin>>numerator;
cout<<"\n Enter the denominator :";
cin>>denominator;
try
{
if(denominator==0)
throw "\n\n\tDivision by zero is impossible\n\n";
else
divide=numerator/denominator;
cout<<"\n\n\t"<<numerator<<" / "<<denominator<<" =
"<<(numerator/denominator)<<"\n\n";
}
catch(const char* x)
{
cout<<x;
}
return 0;
}
OUTPUT

You might also like