C++ Example Programs
Introduction to C++
C++ is a cross-platform language that can be used to create high-performance
applications. It was developed by Bjarne Stroustrup, as an extension to the C
language. The language was updated 3 major times in 2011, 2014, and 2017 to
C++11, C++14, and C++17.
Why Use C++?
1. C++ is one of the world’s most popular programming languages.
2. C++ can be found in today’s operating systems, Graphical User
Interfaces, and embedded systems.
3. C++ is an object-oriented programming language that gives a clear
structure to programs and allows code to be reused, lowering
development costs.
4. C++ is portable and can be used to develop applications that can be
adapted to multiple platforms.
5. C++ is fun and easy to learn!
6. As C++ is close to C# and Java, it makes it easy for programmers to
switch to C++ or vice versa.
Basic C++ Syntax / Structure of C++ Program
// First simple C++ program without class
#include <iostream> // header file
using namespace std;
int main() // main function
{
cout << "Hello World!\n";
return 0;
}
Programs are a sequence of instructions or statements. These statements
form the structure of a C++ program.
C++ program structure is divided into various sections, namely, headers,
class definition, member functions definitions and main function.
1 A simple C++ program (without a class) includes comments, headers,
namespace, main() and input/output statements.
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
Comments are a vital element of a program that is used to increase the
readability of a program and to describe its functioning. Comments are not
executable statements and hence, do not increase the size of a file.
Components of a C++ Code:
1. #include<iostream>: #include is the pre-processor directive that is used to
include files in our program. Here we are including the iostream standard
file which is necessary for the declarations of basic standard input/output
library in C++.
2. Using namespace std: All elements of the standard C++ library are
declared within a namespace. Here we are using the std namespace.
3. int main(): The execution of any C++ program starts with the main
function, hence it is necessary to have a main function in your program.
‘int’ is the return value of this function.
4. {}: The curly brackets are used to indicate the starting and ending point of
any function. Every opening bracket should have a corresponding closing
bracket.
5. cout<<”Hello World!\n”; This is a C++ statement. cout represents the
standard output stream in C++. It is declared in the iostream standard file
within the std namespace. The text between quotations will be printed on
the screen. \n will not be printed, it is used to add a line break. Each
statement in C++ ends with a semicolon (;).
6. return 0; return signifies the end of a function. Here the function is main,
so when we use return 0, it exits the program. We are returning 0 because
we mentioned the return type of the main function as integer (int main). A
zero indicates that everything went fine and one indicates that something
has gone wrong.
// simple C++ program with class and object
#include<iostream.h>
#include<conio.h>
class person
{
char name[30];
int age;
public:
2
void getdata(void);
void putdata(void);
};
void person::getdata(void)
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
{
cout<<"Enter the name of a person\n";
cin>>name;
cout<<"Enter the age of a person\n";
cin>>age;
}
void person::putdata(void)
{
cout<<"Name: "<<name<<"\n";
cout<<"Age: "<<age<<"\n";
}
int main()
{
clrscr();
person p; // object p is created for the class person
p.getdata(); // calling member function getdata() using object
p.putdata(); // calling member function putdata() using object
getch();
return 0;
}
***************************************************************
Polymorphism
Compile time Polymorphism
#include<iostream.h>
#include<conio.h>
class poly
{
public:
void add(int a, int b)
{
cout<<"Sum = "<<a+b<<endl;
}
void add(int a, int b,int c)
{
cout<<"Sum = "<<a+b+c<<endl;
}
void add(double a, double b)
3
{
cout<<"Sum = "<<a+b<<endl;
}
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
};
int main()
{
poly p;
p.add(10,23);
p.add(14,23,45);
p.add(4.8,9.9);
getch();
return 0;
}
Run time Polymorphism
#include<iostream.h>
#include<conio.h>
class Base
{
public:
void display()
{
cout<<"This is Base class"<<endl;
}
};
class Derived : public Base
{
public:
void display()
{
cout<<"This is Derived class"<<endl;
}
};
int main()
{
clrscr();
Base b;
Derived d; // derived class object
//b.display();
d.display(); // calls display() function from derived class
d.Base::display(); // calls display() function from base class
getch();
4 return 0;
}
****************************************************************
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
Functions:
Program to demonstrate function prototype
#include<iostream.h>
float volume(float); //prototype
int main()
{
float a,v;
cout<<"Enter the sides of the cube\n";
cin>>a;
v = volume(a); //function call
cout<<"Volume of a cube for a given side is : "<<v;
return 0;
}
float volume(float x) // called function
{
float res;
res = x*x*x;
return res;
}
Function call by reference to swap two numbers
#include<iostream.h>
#include<conio.h>
void swap(int &,int &); // function prototype
int main()
{
int a, b;
clrscr();
cout<<"Enter the values of a and b\n";
cin>>a>>b;
cout<<"Values of a and b before swapping\n";
cout<<"a = "<<a<<"\nb = "<<b<<endl;
swap(a,b); //function call
cout<<"Values of a and b after swapping\n";
cout<<"a = "<<a<<"\nb = "<<b<<endl;
getch();
return 0;
}
void swap(int &m,int &n) //called function by reference
{
5 int temp;
temp = m;
m = n;
n = temp;
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
}
****************************************************************
Inline functions
Program to demonstrate inline functions.
#include<iostream.h>
#include<conio.h>
inline double volume(double x)
{
return(x*x*x);
}
int main()
{
double cube1,cube2,a;
clrscr();
cout<<"Enter the value of side\n";
cin>>a;
cube1 = volume(a);
cout<<"The volume of cube1 is :"<<cube1<<endl;
cube2 = volume(1.5+3.7);
cout<<"The volume of cube2 is :"<<cube2<<endl;
getch();
return 0;
}
Example program to demonstrate default arguments
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
int main()
{
float amount;
float value(float p,int n, float r=0.15); //prototype
void printline(char c='*',int len=40); //prototype
6
clrscr();
printline(); //uses default arguments specified in prototype
amount = value(5000.0,5); // uses default third argument(r=0.15) from prototype
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
cout<<"\nFinal value = "<<amount <<"\n\n";
printline('='); // uses one default value(len=40) from prototype
getch();
return 0;
}
float value(float p,int n,float r)
{
int year = 1;
float sum =p;
while(year<=n)
{
sum = sum*(1+r);
year = year+1;
}
return(sum);
}
void printline(char ch,int len)
{
for(int i=1;i<=len;i++)
printf("%c",ch);
printf("\n");
}
OUTPUT
****************************************
Final value = 10056.786133
========================================
Constructors:
Program to demonstrate multiple constructors
#include<iostream.h>
#include<conio.h>
class integer
{
int m,n;
7 public:
integer() // Default constructor
{
m = 10;
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
n = 20;
}
integer(int a,int b) // Parameterized constructor
{
m = a;
n = b;
}
integer(integer &i) // Copy constructor
{
m = i.m;
n = i.n;
}
void putdata()
{
cout<<"m = "<<m<<endl;
cout<<"n = "<<n<<endl;
}
};
int main()
{
clrscr();
integer i1;
cout<<"Inside default constructor\n";
i1.putdata();
integer i2(25,55);
cout<<"Inside parameterized constructor\n";
i2.putdata();
integer i3(i2);
cout<<"Inside copy constructor\n";
i3.putdata();
getch();
return 0;
}
****************************************************************
Program to add two complex numbers using constructor overloading.
#include<iostream.h>
#include<conio.h>
class complex
8 {
float x,y;
public:
complex() // Default constructor
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
{ }
complex(float a) // Parameterized constructor with one parameter
{
x = y = a;
}
complex(float real, float imag) // Parameterized constructor with
two parameters
{
x = real;
y = imag;
}
friend complex sum(complex, complex);
friend void show(complex);
};
complex sum(complex c1, complex c2)
{
complex c3;
c3.x = c1.x + c2.x;
c3.y = c1.y + c2.y;
return(c3);
}
void show(complex c)
{
cout<< c.x << "+" << c.y <<"i"<<endl;
}
int main()
{
clrscr();
complex a(2.7,3.5);
complex b(1.6);
complex c;
c = sum(a,b);
cout<<" a = ";
show(a);
cout<<" b = ";
show(b);
cout<<" c = ";
show(c);
getch();
9 return 0;
}
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
Destructors
Program to demonstrate to constructor and destructor.
#include<iostream.h>
#include<conio.h>
int count =0;
class alpha
public:
alpha() //constructor
count++;
cout<<"object A"<<count<<" created\n";
~alpha() //destructor
cout<<"object A"<<count<<" Destroyed\n";
count--;
};
int main()
{
10 clrscr();
cout<< "\n\nEnter Main\n";
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
alpha A1,A2,A3,A4;
cout<<"\nEnter Block-1\n";
alpha A5;
cout<<"\nEnter Block-2\n";
alpha A6;
cout<<"\nRE-ENTER Main\n";
return 0;
***************************OUTPUT****************************
Enter Main
object A1 created
object A2 created
object A3 created
object A4 created
11 Enter Block-1
object A5 created
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
object A5 Destroyed
Enter Block-2
object A5 created
object A5 Destroyed
RE-ENTER Main
object A4 Destroyed
object A3 Destroyed
object A2 Destroyed
object A1 Destroyed
Single Inheritance: one base class and one derived class with public
derivation.
#include<iostream.h>
#include<conio.h>
class Base
int a;
public:
int b;
void get_ab();
12 int get_a(void);
void show_a(void);
};
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
class Derived:public Base
int c;
public:
void mul(void);
void display(void);
};
void Base::get_ab()
a=10;
b=25;
int Base::get_a(void)
return a;
void Base::show_a()
cout<<"a = "<<a<<"\n";
void Derived::mul()
{
13 c = b*get_a();
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
void Derived::display()
cout<<"a = "<<get_a() <<"\n";
cout<<"b = "<<b<<"\n";
cout<<"c = "<<c<<"\n";
int main()
clrscr();
Derived d;
d.get_ab();
d.mul();
d.show_a();
d.display();
getch();
return 0;
************************ OUTPUT*************************
a = 10 //d.show_a()
a = 10 //d.display()
b = 25 //d.display()
c = 250 //d.display()
14
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
Multiple inheritance: two base classes inherited by one derived class.
#include<iostream.h>
#include<conio.h>
class Base1
protected:
int a;
public:
void get_a(int);
};
class Base2
protected:
int b;
public:
void get_b(int);
};
class Derived:public Base1,public Base2 //multiple inheritance
public:
void display();
};
15 void Base1::get_a(int x)
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
a = x;
void Base2::get_b(int y)
b = y;
void Derived::display()
cout<<"a = "<<a<<"\n"; //inherited from Base1
cout<<"b = "<<b<<"\n"; //inherited from Base2
cout<<"a*b = "<<a*b<<"\n";//multiplying inherited data in derived class
int main()
clrscr();
Derived d;
d.get_a(10);
d.get_b(15);
d.display();
getch();
return 0;
}
16 ************************OUTPUT**************
a = 10
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire
C++ Example Programs
b = 15
a*b = 150
****************************************************
17
Ravikumar, Assistant Professor | Dept. of CSE, SDMIT, Ujire