Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » Programs » Top C++ programs for geeks | C++ code examples

  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • Subhas Chandra Bose Biography in Hindi, Essay, Paragraph
    Subhas Chandra Bose Biography in Hindi, Essay, Paragraph Biography
  • National Doctors Day in India and Other Countries, July 1, 2022
    National Doctors Day in India and Other Countries, July 1, 2022 General Knowledge
  • World Earth Day in Hindi | पृथ्वी दिवस कब और क्यों मनाया जाता है?
    Earth Day in Hindi, Theme | पृथ्वी दिवस कब और क्यों मनाया जाता है? General Knowledge
  • Pythagorean Theorem in Hindi, Definition
    Pythagorean Theorem in Hindi, Definition, Formula, Proof पाइथागोरस थ्योरम क्या है जानिए हिंदी में? Science
  • Real life Inspirational Stories in Hindi | Success story in Hindi
    Real life Inspirational Stories in Hindi | Success story in Hindi Biography
  • What is Verb
    What is Verb? Types of Verbs and Examples Grammar
  • What is Noun
    What is a Noun? 5 Types of Nouns with Examples, Noun diagram Grammar

Top C++ programs for geeks | C++ code examples

Posted on September 1, 2021March 22, 2022 By GeekCer Education No Comments on Top C++ programs for geeks | C++ code examples
Top C++ programs for geeks

Top C++ programs for geeks for interview that will help you to understand the basic logic and by using this you can write more programs.

We’ve put up a collection of simple and complex C++ program for beginners and professionals to learn with.

Table of Contents

  • Print Hello World in C++
  • Enter two numbers and find addition in C++.
  • Input any character and print on screen.
  • Enter any string and print on screen.
  • Enter any string and display using read and write method.
  • Program to display decimal, octal and hexadecimal value.
  • Program to perform operation using variable.
  • Initialize the variables in constructor initialization way.
  • Program of string assignment in C++.
  • Find the greater number using conditional operator.
  • Program to enter any number and display.
  • Write a program to input string and display using cin.
  • Input information and display using stringstream.
  • Program to implement countdown using while loop.
  • Program for compound assignment operators.
  • Write a program to break loop using break statement.
  • Write a program by using continue statement.
  • C++ program using goto statement.
  • Program using function to add two numbers.
  • Write a program of void function.
  • Program for passing parameter by reference.
  • C++ program to return more than one values from function.
  • C++ program to pass default value in the function.
  • Write a program of function overloading.
  • Write a program of functions prototypes.
  • Program to initialize and access the array.
  • C++ Program to pass arrays as parameters.
  • Write a program of pointer and array.
  • Write a C++ program of pointer to function.
  • C++ program of pointer to structures.
  • Write a program to calculate area of rectangle using class.
  • C++ program of class and use multiple object.
  • Program using constructors and destructors in C++.
  • C++ program to initialize fields using constructor.
  • Write a C++ program of constructor overloading.
  • Write a C++ program of operator overloading.
  • Program by using static members in class.
  • Write of program of friend function in C++.
  • Write of program of friend class in C++.
  • C++ program of single level inheritance.
  • Program of multiple inheritance in C++.
  • Write a program of pointers to base class in C++.
  • Write a program to implement virtual function.
  • Program to call pure virtual function from abstract base class.
  • Write a program of abstract base class.
  • Program of function template in C++.
  • Program of class template in C++
  • Write a program to by using namespaces.
  • Program to show usage of C++ “using” keyword.
  • Write a program using try-catch statement exception handling in C++.
  • C++ program of standard exception handling.
  • Write a program of class type casting in C++.
  • Write a program using typeid in C++.
  • C++ Program of file operation.
  • Write a program of writing on a text file.
  • Program to read a text file in C++.
  • Write a program find the file size.
  • Write a program to uniquely identify a namespace with the keyword namespace.

Print Hello World in C++

                  #include <iostream>
using namespace std;
int main() 
{
  cout << "Hello World!";
  return 0;
}

Output:
Hello World!

Enter two numbers and find addition in C++.

                  #include <iostream>
using namespace std;
int main() 
{
  int a;
  int b;
  cout<<"Enter two numbers: ";
  cin>>a;
  cin>>b;
  cout<<"Sum of two numbers is "<<a+b;
  return 0;
}

Output:
Enter two numbers: 10
20
Sum of two numbers is 30

Input any character and print on screen.

                  #include <iostream>
using namespace std;
int main() 
{
  char c;
  cout<<"Enter any character: ";
  cin.get(c);
  cout<<"Entered character is: ";
  cout.put(c);
  return 0;
}

Output:
Enter any character: G
Entered character is: G

Enter any string and print on screen.

                  #include <iostream>
using namespace std;
int main() 
{
  char c[15];
  cout<<"Enter any String: ";
  cin.getline(c, 15);
  cout<<"Entered string is: "<< c << endl;
  cout.write(c, 15);
  return 0;
}

Output:
Enter any String: GeekCer!
Entered string is: GeekCer!
GeekCer!

Enter any string and display using read and write method.

                  #include <iostream>
using namespace std;
int main() 
{
  char name[20];
  cout<<"Enter any String: ";
  cin.read(name, 20);
  cout<<"Entered string is: "<< name << endl;
  cout.write(name, 20);
  return 0;
}

Output:
Enter any String: Welcome To GeekCer!
Entered string is: Welcome To GeekCer!

Welcome To GeekCer!

Program to display decimal, octal and hexadecimal value.

                  #include <iostream>
#include <iomanip>
using namespace std;
int main() 
{
  int value = 10;
  cout<<"Decimal base: "<<setbase(10)<<value;
  cout<<"\nHexadecimal base: "<<setbase(16)<<value;
  cout<<"\nOctal base: "<<setbase(8)<<value;
  return 0;
}

Output:
Decimal base: 10
Hexadecimal base: a
Octal base: 12

Program to perform operation using variable.

                  #include <iostream>
using namespace std;
int main() 
{
  int a, b;
  int result;
  a = 15;
  b = 5;
  a = a + 1;
  result = a - b;
  cout << "Result: " << result;
  return 0;
}

Output:
Result: 11

Initialize the variables in constructor initialization way.

                  #include <iostream>
using namespace std;
int main() 
{
  int a = 10; // initial value = 10
  int b(2); // constructor initialization = 2
  int result; 
  a = a + 3;
  result = a - b;
  cout <<"Result: " << result;
  return 0;
}

Output: (You can use Dev C++ compiler)
Result: 11

Program of string assignment in C++.

                  #include <iostream>
#include <string>
using namespace std;
int main() 
{
  string testString;
  testString = "This is the first string content";
  cout << testString << endl;
  testString = "This is a second string content";
  cout << testString << endl;
  return 0;
}

Output:
This is the first string content
This is a second string content

Find the greater number using conditional operator.

                  #include <iostream>
#include <string>
using namespace std;
int main() 
{
  int a, b, c;
  a = 10;
  b = 20;
  c = (a > b) ? a : b;
  cout<<"Greater number is "<< c;
  return 0;
}

Output:
Greater number is 20

Program to enter any number and display.

                  #include <iostream>
using namespace std;
int main() 
{
  int n;
  cout << "Please enter any number: ";
  cin >> n;
  cout << "The number you entered is " << n;
  return 0;
}

Output:
Please enter any number: 100
The number you entered is 100

Know more about Top HR Interview Questions and Answers for Job Seekers

Write a program to input string and display using cin.

                  #include <iostream>
#include <string>
using namespace std;
int main() 
{
  string str;
  cout << "What is your name? ";
  getline (cin, str);
  cout << "Hello " << str << ".\n";
  cout << "What is your favorite sport? ";
  getline (cin, str);
  cout << "I like " << str << " too!\n";
  return 0;
}

Output:
What is your name? Cyber Geek
Hello Cyber Geek.
What is your favorite sport? Cricket
I like Cricket too!

Input information and display using stringstream.

                  #include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() 
{
  string str;
  float price = 0;
  int quantity = 0;
  cout << "Enter price: ";
  getline (cin, str);
  stringstream(str) >> price;
  cout << "Enter quantity: ";
  getline (cin, str);
  stringstream(str) >> quantity;
  cout << "Total price: " << price*quantity << endl;
  return 0;
}

Output:
Enter price: 5000
Enter quantity: 20
Total price: 100000

Program to implement countdown using while loop.

                  #include <iostream>
using namespace std;
int main() 
{
  int n;
  cout << "Enter the starting number : ";
  cin >> n;
  while (n > 0) 
  {
    cout << n << ", ";
    --n;
  }
  cout << "Finish!\n";
  return 0;
}

Output:
Enter the starting number : 10
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Finish!

Program for compound assignment operators.

                  #include <iostream>
using namespace std;
int main() 
{
  int a, b=5;
  a = b;
  a += 2; // Equivalent to a=a+2
  cout << "Result: " << a;
  return 0;
}

Output:
Result: 7

Write a program to break loop using break statement.

                  #include <iostream>
using namespace std;
int main() 
{
  int i;
  for (i = 10; i > 0; i--)
  {
    cout << i << ", ";
    if (i == 5)
    {
      cout << "Countdown Aborted!";
      break;
    }
  }
  return 0;
}

Output:
10, 9, 8, 7, 6, 5, Countdown Aborted!

Write a program by using continue statement.

                  #include <iostream>
using namespace std;
int main() 
{
  int i;
  for (i = 10; i > 0; i--)
  {
    if (i == 3) // Skipped 3
    {
      continue;
    }
    cout << i << ", ";
  }
  cout << "Finish!";
  return 0;
}

Output:
10, 9, 8, 7, 6, 5, 4, 2, 1, Finish!

C++ program using goto statement.

                  #include <iostream>
using namespace std;
int main() 
{
  int i = 10;
  loop:
  cout << i << ", ";
  i--;
  if (i > 0) goto loop;
  cout << "Finish!";
  return 0;
}

Output:
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Finish!

Program using function to add two numbers.

                  #include <iostream>
using namespace std;
int addition (int a, int b)
{
  int sum;
  sum = a + b;
  return sum;
}
int main() 
{
  int sum;
  sum = addition (10, 30);
  cout <<"Addition is "<< sum;
  return 0;
}

Output:
Addition is 40

Write a program of void function.

                  #include <iostream>
using namespace std;
void displayMessage ()
{
  cout << "This is a function!";
}
int main() 
{
  displayMessage ();
  return 0;
}

Output:
This is a function!

Program for passing parameter by reference.

                  #include <iostream>
using namespace std;
void call (int& a, int& b, int& c)
{
  a *= 3;
  b *= 2;
  c *= 4;
}
int main() 
{
  int x=1, y=3, z=7;
  call (x, y, z);
  cout << "x=" << x << ", y=" << y << ", z=" << z;
  return 0;
}

Output:
x=3, y=6, z=28

You may also like Top CPrograms for geeks

C++ program to return more than one values from function.

                  #include <iostream>
using namespace std;
void getvalue (int x, int& prev, int& next)
{
  prev = x-1;
  next = x+1;
}
int main() 
{
  int x = 10, y, z;
  getvalue (x, y, z);
  cout << "Previous=" << y << ", Next=" << z;
  return 0;
}

Output:
Previous=9, Next=11

C++ program to pass default value in the function.

                  #include <iostream>
using namespace std;
int multiplication (int a, int b=2)
{
  int result;
  result  =a * b;
  return result;
}
int main() 
{
  cout << multiplication (10);
  cout << endl;
  cout << multiplication (20, 4);
  return 0;
}

Output:
20
80

Write a program of function overloading.

                  #include <iostream>
using namespace std;
int calculate (int a, int b)
{
  return (a + b);
}
float calculate (float a, float b)
{
  return (a / b);
}
int main() 
{
  int x=10, y=20;
  float n=25.0, m=5.0;
  cout <<"Addition: "<< calculate (x, y);
  cout << "\n";
  cout <<"Division: "<< calculate (n, m);
  return 0;
}

Output:
Addition: 30
Division: 5

Write a program of functions prototypes.

                  #include <iostream>
using namespace std;
void odd (int a);
void even (int a);
int main() 
{
  int n;
  do {
    cout << "Type a number (0 to exit): ";
    cin >> n;
    if (n != 0) 
    {
      odd (n);
    }
  } while (n!=0);
  return 0;
}
void odd (int a)
{
  if (a % 2 != 0) 
    cout << "Number is odd.\n";
  else 
    even (a);
}
void even (int a)
{
  if (a % 2==0) 
    cout << "Number is even.\n";
  else 
    odd (a);
}

Output:
Type a number (0 to exit): 2
Number is even.
Type a number (0 to exit): 6
Number is even.
Type a number (0 to exit): 7
Number is odd.
Type a number (0 to exit): 5
Number is odd.
Type a number (0 to exit): 11
Number is odd.
Type a number (0 to exit): 0

Program to initialize and access the array.

                  #include <iostream>
using namespace std;
int main() 
{
  int arr [] = {10, 20, 30, 40, 50};
  int n, sum=0;
  for ( n=0 ; n<5 ; n++ )
  {
    sum += arr[n];
  }
  cout <<"Sum: " << sum;
  return 0;
}

Output:
Sum: 150

C++ Program to pass arrays as parameters.

                  #include <iostream>
using namespace std;
void display (int arg[], int length) 
{
  for (int i=0; i < length; i++)
  cout << arg[i] << " ";
  cout << "\n";
}
int main() 
{
  int arr1[] = {15, 10, 15};
  int arr2[] = {20, 4, 60, 8, 10};
  display (arr1, 3);
  display (arr2, 5);
  return 0;
}

Output:
15 10 15
20 4 60 8 10

Write a program of pointer and array.

                  #include <iostream>
using namespace std;
int main() 
{
  int numbers[5];
  int * p;
  p = numbers; *p = 5;
  p++; *p = 20;
  p = &numbers[2]; *p = 30;
  p = numbers + 3; *p = 40;
  p = numbers; *(p+4) = 50;
  for (int i=0; i < 5; i++) 
  {
    cout << numbers[i] << ", ";
  }
  return 0;
}

Output:
5, 20, 30, 40, 50,

Write a C++ program of pointer to function.

                  #include <iostream>
using namespace std;
int addition (int a, int b)
{ 
  return (a + b); 
}
int subtraction (int a, int b)
{ 
  return (a - b); 
}
int operation (int x, int y, int(*functocall)(int,int))
{
  int g;
  g = (*functocall)(x,y);
  return g;
}
int main() 
{
  int m, n;
  int (*minus)(int,int) = subtraction;
  m = operation (7, 5, addition);
  n = operation (20, m, minus);
  cout <<"Result: " << n;
  return 0;
}

Output:
Result: 8

Top 100 C++ programs for geeks

C++ program of pointer to structures.

                  #include <iostream>
#include <sstream>
#include <string>
using namespace std;
struct movies 
{
  string title;
  int year;
};
int main() 
{
  string str;
  movies amovie;
  movies * pmovie;
  pmovie = &amovie;
  cout << "Enter title: ";
  getline (cin, pmovie->title);
  cout << "Enter year: ";
  getline (cin, str);
  (stringstream) str >> pmovie->year;
  cout << "\nYou have entered:\n";
  cout << pmovie->title;
  cout << " (" << pmovie->year << ")\n";
  return 0;
}

Output:
Enter title: Baahubali
Enter year: 2015

You have entered:
Baahubali (2015)

Write a program to calculate area of rectangle using class.

                  #include <iostream>
using namespace std;
class Rectangle
{
  int x, y;
  public:
  void setValues (int,int);
  int area () 
  {
    return (x*y);
  }
};
void Rectangle::setValues (int a, int b) 
{
  x = a;
  y = b;
}
int main() 
{
  Rectangle rect;
  rect.setValues (10, 20);
  cout << "Area: " << rect.area();
  return 0;
}

Output:
Area: 200

C++ program of class and use multiple object.

                  #include <iostream>
using namespace std;
class Rectangle
{
  int x, y;
  public:
  void setValues (int,int);
  int area () 
  {
    return (x*y);
  }
};
void Rectangle::setValues (int a, int b) 
{
  x = a;
  y = b;
}
int main() 
{
  Rectangle rect;
  Rectangle rectb;
  rect.setValues (10, 20);
  rectb.setValues (5, 6);
  cout << "Area using first object: " << rect.area();
  cout << endl;
  cout << "Area using second object: " << rectb.area();
  return 0;
}

Output:
Area using first object: 200
Area using second object: 30

Program using constructors and destructors in C++.

                  #include <iostream>
using namespace std;
class Rectangle 
{
  int *width, *height;
  public:
  Rectangle (int,int);
  ~Rectangle ();
  int area () 
  {
    return (*width * *height);
  }
};
Rectangle::Rectangle (int a, int b) 
{
  width = new int;
  height = new int;
  *width = a;
  *height = b;
}
Rectangle::~Rectangle () 
{
  delete width;
  delete height;
}
int main() 
{
  Rectangle rect (30, 40), rectb (15, 20);
  cout << "First rectangle area: " << rect.area() << endl;
  cout << "Second rectangle area: " << rectb.area();
  return 0;
}

Output:
First rectangle area: 1200
Second rectangle area: 300

C++ program to initialize fields using constructor.

                  #include <iostream>
using namespace std;
class Rectangle 
{
  int width, height;
  public:
  Rectangle (int,int);
  int area () 
  {
    return (width*height);
  }
};
Rectangle::Rectangle (int a, int b) {
  width = a;
  height = b;
}
int main() 
{
  Rectangle rect (20, 10);
  cout << "Area of rectangle: " << rect.area();
  return 0;
}

Output:
Area of rectangle: 200

Write a C++ program of constructor overloading.

                  #include <iostream>
using namespace std;
class Rectangle 
{
  int width, height;
  public:
  Rectangle ();
  Rectangle (int, int);
  int area () 
  {
    return width * height;
  }
};
Rectangle::Rectangle () 
{
  width = 10;
  height = 20;
}
Rectangle::Rectangle (int a, int b) 
{
  width = a;
  height = b;
}
int main() 
{
  Rectangle rect, rectb (15, 20);
  cout << "First rectangle area: " << rect.area() << endl;
  cout << "Second rectangle area: " << rectb.area();
  return 0;
}

Output:
First rectangle area: 200
Second rectangle area: 300

Write a C++ program of operator overloading.

                  #include <iostream>
using namespace std;
class GeekCer 
{
  public:
  int x,y;
  GeekCer () {};
  GeekCer (int,int);
  GeekCer operator + (GeekCer);
};
GeekCer::GeekCer (int a, int b) 
{
  x = a;
  y = b;
}
GeekCer GeekCer::operator+ (GeekCer param) 
{
  GeekCer temp;
  temp.x = x + param.x;
  temp.y = y + param.y;
  return temp;
}
int main() 
{
  GeekCer a (10, 20);
  GeekCer b (5, 4);
  GeekCer c;
  c = a + b;
  cout << c.x << ", " << c.y;
  return 0;
}

Output:
15, 24

Program by using static members in class.

                  #include <iostream>
using namespace std;
class Geek 
{
  public:
  static int n;
  Geek () 
  { 
    n++; 
  };
  ~Geek () 
  { 
    n--; 
  };
};
int Geek::n=0;
int main() 
{
  Geek a;
  Geek b[10];
  Geek * c = new Geek;
  cout << a.n << endl;
  delete c;
  cout << Geek::n << endl;
  return 0;
}

Output:
12
11

Write of program of friend function in C++.

                  #include <iostream>
using namespace std;
class Rectangle 
{
  int width, height;
  public:
  void set_values (int, int);
  int area () 
  {
    return (width * height);
  }
  friend Rectangle duplicate (Rectangle);
};
void Rectangle::set_values (int a, int b) 
{
  width = a;
  height = b;
}
Rectangle duplicate (Rectangle rectparam)
{
  Rectangle rectres;
  rectres.width = rectparam.width * 2;
  rectres.height = rectparam.height * 2;
  return (rectres);
}
int main() 
{
  Rectangle rect, rectb;
  rect.set_values (10, 20);
  rectb = duplicate (rect);
  cout <<"Area is "<< rectb.area();
  return 0;
}

Output:
Area is 800

Write of program of friend class in C++.

                  #include <iostream>
using namespace std;
class Square;
class Rectangle 
{
  int width, height;
  public:
  int area ()
  {
    return (width * height);
  }
  void convert (Square a);
};
class Square 
{
  private:
  int side;
  public:
  void set_side (int a)
  {
    side = a;
  }
  friend class Rectangle;
};
void Rectangle::convert (Square a) 
{
  width = a.side;
  height = a.side;
}
int main() 
{
  Square sqr;
  Rectangle rect;
  sqr.set_side(10);
  rect.convert(sqr);
  cout <<"Area is " << rect.area();
  return 0;
}

Output:
Area is 100

C++ program of single level inheritance.

                  #include <iostream>
using namespace std;
class Polygon 
{
  protected:
  int width, height;
  public:
  void setValues (int a, int b)
  { 
    width = a; 
    height = b;
  }
};
class Rectangle: public Polygon 
{
  public:
  int area ()
  { 
    return (width * height); 
  }
};
class Triangle: public Polygon 
{
  public:
  int area ()
  { 
    return (width * height / 2); 
  }
};
int main() 
{
  Rectangle rect;
  Triangle trgl;
  rect.setValues (10, 20);
  trgl.setValues (5, 10);
  cout <<"Area of rectangle is "<< rect.area() << endl;
  cout <<"Area of triangle is "<< trgl.area() << endl;
  return 0;
}

Output:
Area of rectangle is 200
Area of triangle is 25

Program of multiple inheritance in C++.

                  #include <iostream>
using namespace std;
class Polygon 
{
  protected:
  int width, height;
  public:
  void setValues (int a, int b)
  { 
    width = a; 
    height = b;
  }
};
class Output 
{
  public:
  void output (int i);
};
void Output::output (int i) 
{
  cout << i << endl;
}
class Rectangle: public Polygon, public Output 
{
  public:
  int area ()
  { 
    return (width * height); 
  }
};
class Triangle: public Polygon, public Output 
{
  public:
  int area ()
  { 
    return (width * height / 2); 
  }
};
int main() 
{
  Rectangle rect;
  Triangle trgl;
  rect.setValues (10, 20);
  trgl.setValues (10, 10);
  rect.output (rect.area());
  trgl.output (trgl.area());
  return 0;
}

Output:
200
50

Write a program of pointers to base class in C++.

                  #include <iostream>
using namespace std;
class Polygon 
{
  protected:
  int width, height;
  public:
  void setValues (int a, int b)
  { 
    width=a; 
    height=b; 
  }
};
class Rectangle: public Polygon 
{
  public:
  int area ()
  { 
    return width * height; 
  }
};
class Triangle: public Polygon 
{
  public:
  int area ()
  { 
    return width * height / 2; 
  }
};
int main() 
{
  Rectangle r;
  Triangle t;
  Polygon * ppoly1 = &r;
  Polygon * ppoly2 = &t;
  ppoly1->setValues (10, 50);
  ppoly2->setValues (10, 25);
  cout << r.area() << endl;
  cout << t.area() << endl;
  return 0;
}

Output:
500
125

Write a program to implement virtual function.

                  #include <iostream>
using namespace std;
class Polygon 
{
  protected:
  int width, height;
  public:
  void setValues (int a, int b)
  { 
  width=a; 
  height=b; 
  }
  virtual int area ()
  { 
    return (0); 
  }
};
class Rectangle: public Polygon 
{
  public:
  int area ()
  { 
    return (width * height); 
  }
};
class Triangle: public Polygon 
{
  public:
  int area ()
  { 
    return width * height / 2; 
  }
};
int main() 
{
  Rectangle r;
  Triangle t;
  Polygon p;
  Polygon * poly1 = &r;
  Polygon * poly2 = &t;
  Polygon * poly3 = &p;
  poly1->setValues (10,50);
  poly2->setValues (20,50);
  poly3->setValues (30,50);
  cout<< poly1->area()<< endl;
  cout<< poly2->area()<< endl;
  cout<< poly3->area()<< endl;
  return 0;
}

Output:
500
500
0

Program to call pure virtual function from abstract base class.

                  #include <iostream>
using namespace std;
class Polygon 
{
  protected:
  int width, height;
  public:
  void setValues (int a, int b)
  { 
  width = a; 
  height = b; }
  virtual int area (void) =0;
  void printarea (void)
  { 
    cout << this->area() << endl; 
  }
};
class Rectangle: public Polygon 
{
  public:
  int area (void)
  { 
    return (width * height); 
  }
};
class Triangle: public Polygon 
  {
  public:
  int area (void)
  { 
    return (width * height / 2); 
  }
};
int main() 
{
  Rectangle r;
  Triangle t;
  Polygon * poly1 = &r;
  Polygon * poly2 = &t;
  poly1->setValues (40, 50);
  poly2->setValues (10, 20);
  poly1->printarea();
  poly2->printarea();
  return 0;
}

Output:
2000
100

Write a program of abstract base class.

                  #include <iostream>
using namespace std;
class Polygon 
{
  protected:
  int width, height;
  public:
  void setValues (int a, int b)
  { 
    width=a; 
    height=b; 
  }
  virtual int area (void) =0;
};
class Rectangle: public Polygon 
{
  public:
  int area (void)
  { 
    return (width * height); 
  }
};
class Triangle: public Polygon 
{
  public:
  int area (void)
  { 
    return (width * height / 2); 
  }
};
int main() 
{
  Rectangle r;
  Triangle t;
  Polygon * poly1 = &r;
  Polygon * poly2 = &t;
  poly1->setValues (40, 25);
  poly2->setValues (20, 50);
  cout << poly1->area() << endl;
  cout << poly2->area() << endl;
  return 0;
}

Output:
1000
500

Program of function template in C++.

                  #include <iostream>
using namespace std;
template <class T> T GetMax (T a, T b) 
{
  T result;
  result = (a>b)? a : b;
  return result;
}
int main() 
{
  int i = 10, j = 20, k;
  long l = 10, m = 50, n;
  k = GetMax<int>(i, j);
  n = GetMax<long>(l, m);
  cout << k << endl;
  cout << n << endl;
  return 0;
}

Output:
20
50

Program of class template in C++

                  #include <iostream>
using namespace std;
template <class T> class Pair 
{
  T a, b;
  public:
  Pair (T first, T second)
  {
    a=first; 
    b=second;
  }
  T getmax ();
};
template <class T> T Pair<T>::getmax ()
{
  T retval;
  retval = a > b? a : b;
  return retval;
}
int main() 
{
  Pair <int> obj (200, 705);
  cout << obj.getmax();
  return 0;
}

Output:
705

Write a program to by using namespaces.

                  #include <iostream>
using namespace std;
namespace intvalue
{
  int var = 100;
}
namespace doublevalue
{
  double var = 10.50;
}
int main() 
{
  cout << intvalue::var << endl;
  cout << doublevalue::var << endl;
  return 0;
}

Output:
100
10.5

Program to show usage of C++ “using” keyword.

                  #include <iostream>
using namespace std;
namespace first
{
  int x = 15;
  int y = 10;
}
namespace second
{
  double x = 13.141;
  double y = 21.718;
}
namespace third
{
  double a = 500;
  double b = 803;
}
int main() 
{
  using first::x;
  using second::y;
  using namespace third;
  cout << x << endl;  // value of first namespace
  cout << y << endl;  // value of second namespace
  cout << first::y << endl;  // value of first namespace
  cout << second::x << endl; // value of second namespace
  cout << a << endl;  // value of third namespace
  cout << b << endl;  // value of third namespace
  return 0;
}

Output:
15
21.718
10
13.141
500
803

Write a program using try-catch statement exception handling in C++.

                  #include <iostream>
using namespace std;
int main() 
{
  try
  {
    throw 100;
  }
  catch (int exception)
  {
    cout << "An exception occurred. "<< endl;
    cout << "Exception number is " << exception << endl;
  }
  return 0;
}

Output:
An exception occurred.
Exception number is 100

C++ program of standard exception handling.

                  #include <iostream>
#include <exception>
using namespace std;
class GeekException: public exception
{
  virtual const char* what() const throw()
  {
    return "The exception is generated.";
  }
} ex;
int main() 
{
  try
  {
    throw ex;
  }
  catch (exception& e)
  {
    cout << e.what();
  }
  return 0;
}

Output:
The exception is generated.

Write a program of class type casting in C++.

                  #include <iostream>
using namespace std;
class Dummy 
{
  float i, j;
};
class Addition 
{
  public:
  int result(int x, int y) 
  { 
    return x + y;
  }
};
int main() 
{
  Dummy d;
  Addition * add;
  add = (Addition*) &d;
  cout <<"Result: " << add->result(10, 20);
  return 0;
}

Output:
Result: 30

Write a program using typeid in C++.

                  #include <iostream>
#include <typeinfo>
using namespace std;
int main() 
{
  int * a, b;
  int * x, *y;
  a = 0; 
  b = 0;
  x = 0;
  y = 0;
  if (typeid(a) != typeid(b))
  {
    cout << "Both are of different types.\n";
  }
  if (typeid(x) == typeid(y))
  {
    cout << "Both are of same types.";
  }
  return 0;
}

Output:
Both are of different types.
Both are of same types.

C++ Program of file operation.

                  #include <iostream>
#include <fstream>
using namespace std;
int main() 
{
  ofstream testfile;
  testfile.open ("geek.txt");  // File name
  testfile << "Writing this content to a file.\n";
  testfile.close();
  return 0;
}

Write a program of writing on a text file.

                  #include <iostream>
#include <fstream>
using namespace std;
int main() 
{
  ofstream testfile("geekcer.txt");  // File name
  if (testfile.is_open())
  {
    testfile << "This is a line.\n";
    testfile << "This is another line.\n";
    testfile.close();
  }
  else  
  {
    cout << "Unable to open file";
  }
  return 0;
}

Program to read a text file in C++.

geekcer.txt
This is a line.
This is another line.
                  #include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() 
{
  string line;
  ifstream myfile ("geekcer.txt"); // File name
  if (myfile.is_open())
  {
    while (! myfile.eof() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
  }
  else 
  {
    cout << "Unable to open file";
  }
  return 0;
}

Output:
This is a line.
This is another line.

Write a program find the file size.

geekcer.txt
This is a line.
This is another line.
                  #include <iostream>
#include <fstream>
using namespace std;
int main() 
{
  long begin, end;
  ifstream myfile ("geekcer.txt");
  begin = myfile.tellg();
  myfile.seekg (0, ios::end);
  end = myfile.tellg();
  myfile.close();
  cout << "Size of file is: " << (end-begin) << " bytes.\n";
  return 0;
}

Output:
Size of file is: 40 bytes.

Write a program to uniquely identify a namespace with the keyword namespace.

                  #include <iostream>
using namespace std;
namespace first 
{
  int data = 10;
}
namespace second 
{
  double data = 5.321;
}
int main() 
{
  cout << first::data << endl;
  cout << second::data << endl;
  return 0;
}

Click here to learn Git Commands

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

Programs Tags:Basic c++ programs, C++ programs examples with output, What are C++ programs?

Post navigation

Previous Post: Immutable class in Java | How to create Immutable class in Java
Next Post: Top 100 C programs for geeks

More Related Articles

Immutable class in Java Immutable class in Java | How to create Immutable class in Java Important
Top 100 C programs for geeks Top 100 C programs for geeks C Language
Top Java Programs for Coder Top Java Programs for Coder Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved