Largest of two numbers using nested function
#include<iostream.h>
#include<conio.h>
class set
{
int a,b;
public:
void input(void);
void display(void);
int largest(void);
};
int set :: largest(void)
{
if(a>=b)
return (a);
else
return(b);
}
void set :: input(void)
{
cout<<"Input values of a and b:" << "\n";
cin >> a >>b;
}
void set :: display(void)
{
cout<<"Largest value ="<< largest( ) << "\n";
}
main( )
{
set A;
A.input( );
A.display( );
getch( );
return 0;
}
Array of objects
#include<iostream.h>
#include<conio.h>
class rec
{
private:
int I;
int b;
public:
rec(int a,int c)
{
I=a;
b=c;
}
void put()
{
cout<<"Area is : "<<I*b <<endl;
}
};
void main()
{
clrscr();
rec obj[3]={rec(3,6),rec(2,5),rec(5,5)};
cout<<"Displaying Areas of Rectangles : \n";
for(int i=0;i<3;i++)
obj[i].put();
getch();
}
Inline function
#include<iostream.h>
#include<conio.h>
class line {
public:
inline float mul(float x, float y) {
return (x * y);
inline float cube(float x) {
return (x * x * x);
};
void main() {
line obj;
float val1, val2;
clrscr();
cout << "Enter two values:";
cin >> val1>>val2;
cout << "\nMultiplication value is :" << obj.mul(val1, val2);
cout << "\n\nCube value is :" << obj.cube(val1) << " and "
<< obj.cube(val2);
getch();
}
Function overloading
#include <iostream>
#include<conio.h>
class printData {
public:
void print(int i) {
cout << "Printing int: " << i << endl;
void print(double f) {
cout << "Printing float: " << f << endl;
void print(char* c) {
cout << "Printing character: " << c << endl;
};
int main(void) {
clrscr();
printData pd;
// Call print to print integer
pd.print(5);
// Call print to print float
pd.print(500.263);
// Call print to print character
pd.print("Hello C++");
return 0;
}
Operator overloading
#include<iostream.h>
#include<conio.h>
class NUM
private:
int n;
public:
//function to get number
void getNum(int x)
n=x;
//function to display number
void dispNum(void)
cout << "value of n is: " << n;
//unary ++ operator overloading
void operator ++ (void)
n=++n;
}
//unary -- operator overloading
void operator -- (void)
n=--n;
};
int main()
Clrscr();
NUM num;
num.getNum(10);
++num;
cout << "After increment - ";
num.dispNum();
cout << endl;
--num;
cout << "After decrement - ";
num.dispNum();
cout << endl;
return 0;
}
Constructor
#include <iostream.h>
#include <string.h>
#include <conio.h>
class student
private:
int roll;
char name[50];
public:
student() // default constructor
roll = 0;
strcpy(name," ");
student(char n[50], int r) // user defined constructor
roll = r;
strcpy(name,n);
student(student &s) // copy constructor
roll = s.roll;
strcpy(name,s.name);
void display()
{
cout <<"Name : "<<name<<endl;
cout <<"Roll : "<<roll<<endl;
};
int main()
Clrscr();
student s1; // call default constructor
student s2(5,"John"); // call user defined consructor
student s3(s1); // call copy constructor
cout <<"Display value of s1"<<endl;
s1.display();
cout <<"Display value of s2"<<endl;
s2.display();
cout <<"Display value of s3"<<endl;
s3.display();
s3=s2; // call copy constructor
cout <<"Display value of s3"<<endl;
s3.display();
getch();
return 0;
}
Destructor
#include<iostream.h>
#include<conio.h>
class Marks
public:
int maths;
int science;
//constructor
Marks() {
cout << "Inside Constructor"<<endl;
cout << "C++ Object created"<<endl;
//Destructor
~Marks() {
cout << "Inside Destructor"<<endl;
cout << "C++ Object destructed"<<endl;
};
int main( )
clrscr();
Marks m1;
Marks m2;
return 0;
}
Virtual function
#include<iostream.h>
#include<conio.h>
class shape{
public:
virtual void draw(){
cout<<"shape\n";
};
class rectangle : public shape{
public:
void draw(){
cout<<"rectangle\n";
};
class circle : public shape{
public:
void draw(){
cout<<"circle\n";
}
};
class square : public shape{
public:
void draw(){
cout<<"square\n";
};
int main(){
clrscr();
shape *bptr;
rectangle r;
bptr = &r;
bptr->draw();
circle c;
bptr = &c;
bptr-> draw();
square s;
bptr = &s;
bptr->draw();
return 0;
}
Pointers to object
#include <iostream.h>
#include<conio.h>
class myclass
int i;
public:
void read(int j)
i= j;
int getint()
return i;
};
void main()
clrscr();
myclass ob, *objectPointer;
objectPointer = &ob; // get address of ob
objectPointer->read(10);
cout<<objectPointer->getint(); // use -> to call
getlnt()
getch();
Class object
#include <iostream.h>
#include<conio.h>
#include<string.h>
// Class Declaration
class person {
//Access - Specifier
public:
//Variable Declaration
char name;
int number;
};
//Main Function
int main() {
// Object Creation For Class
Clrscr();
person obj;
//Get Input Values For Object Varibales
cout << "Enter the Name :";
cin >> obj.name;
cout << "Enter the Number :";
cin >> obj.number;
//Show the Output
cout << obj.name << ": " << obj.number << endl;
getch();
return 0;
}
INHERITANCE
#include<iostream.h>
#include<conio.h>
class employee
public:
int salary;
};
class developer : public employee
employee e;
public:
void salary()
cout<<"Enter employee salary: ";
cin>>e.salary; // access base class data member
cout<<"Employee salary: "<<e.salary;
};
void main()
clrscr();
developer obj;
obj.salary();
getch();
}
FRIEND FUNCTION
#include<iostream.h>
#include<conio.h>
class base {
int val1, val2;
public:
void get() {
cout << "Enter two values:";
cin >> val1>>val2;
friend float mean(base ob);
};
float mean(base ob) {
return float(ob.val1 + ob.val2) / 2;
void main() {
clrscr();
base obj;
obj.get();
cout << "\n Mean value is : " << mean(obj);
getch();
}
TEMPLATES
#include<iostream.h>
#include<conio.h>
// template function
template <class T>
T Large(T n1, T n2)
return (n1 > n2) ? n1 : n2;
int main()
clrscr();
int i1, i2;
float f1, f2;
char c1, c2;
cout << "Enter two integers:\n";
cin >> i1 >> i2;
cout << Large(i1, i2) <<" is larger." << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> f1 >> f2;
cout << Large(f1, f2) <<" is larger." << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Large(c1, c2) << " has larger ASCII value.";
return 0;
}
EXCEPTION HANDLING
#include <iostream.h>
#include <conio.h>
int main()
clrscr();
int a,b;
cout << "Enter 2 numbers: ";
cin >> a >> b;
try
if (b != 0)
float div = (float)a/b;
if (div < 0)
throw 'e';
cout << "a/b = " << div;
else
throw b;
catch (int e)
cout << "Exception: Division by zero";
}
catch (char st)
cout << "Exception: Division is less than 1";
catch(...)
cout << "Exception: Unknown";
getch();
return 0;
}
Multilevel inheritence
#include<iostream.h>
#include<conio.h>
class Student{
protected:
int marks;
public:
void accept(){
cout<<" Enter marks";
cin>>marks;
};
class Test :public Student{
protected:
int p=0;
public:
void check(){
if(marks>60){
p=1;
}
};
class Result :public Test{
public:
void print(){
if(p==1)
cout<<"\n You have passed";
else
cout<<"\n You have not passed";
};
int main(){
clrscr();
Result r;
r.accept();
r.check();
r.print();
return 0;
}
Looping in c++
#include<iostream.h>
#include<conio.h>
int main() {
// Variable Declaration
int a;
// Get Input Value
cout << "Enter the Number :";
cin>>a;
//for Loop Block
for (int counter = 1; counter <= a; counter++) {
cout << "Execute " << counter << " time" << endl;
// Wait For Output Screen
getch();
return 0;
}
Prime number
#include<iostream.h>
#include<conio.h>
void main()
int i,no;
clrscr();
cout<<"Enter any num: ";
cin>>no;
if(no==1)
cout<<"Smallest prime num is 2";
for(i=2;i<no;i++)
if(no%i==0)
cout<<"Not prime num";
break;
if(no==i)
cout<<"Prime num";
}
getch();
}
Fibonacci series
#include<iostream.h>
#include<conio.h>
void main()
int i,no, first=0, second=1, next;
clrscr();
first=0;
second=1;
cout<<"Enter nubmer of terms for Series: ";
cin>>no;
cout<<"Fibonacci series are: \n";
for(i=0; i<no; i++)
cout<<"\n"<<first;
next = first + second;
first = second;
second = next;
getch();
}
Reverse an integer
#include <iostream.h>
int main()
int n, reversedNumber = 0, remainder;
cout << "Enter an integer: ";
cin >> n;
while(n != 0)
remainder = n%10;
reversedNumber = reversedNumber*10 + remainder;
n /= 10;
cout << "Reversed Number = " << reversedNumber;
return 0;
}
Call by reference
#include <iostream.h>
#include<conio.h>
// Function prototype
void swap(int*, int*);
int main()
clrscr();
int a = 1, b = 2;
cout << "Before swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
swap(&a, &b);
cout << "\nAfter swapping" << endl;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
return 0;
void swap(int* n1, int* n2) {
int temp;
temp = *n1;
*n1 = *n2;
*n2 = temp;