C++ Programming Examples and Concepts
C++ Programming Examples and Concepts
#include <iostream>
using namespace std;
#include <iostream>
using namespace std;
return 0;
}
003. Enums
#include <iostream>
using namespace std;
/*
typedef type newname;
typedef int wheels;
wheels carwheels;
car_wheels = 4;
sizeof(float);
sizeof(car_wheels);
sizeof(days);
*/
/* syntax */
enum enum_name { list of names } variable_list;
/* example 1 */
enum gender {
female, // 0
male // 1
}G1;
gender G2;
G1 = female;
G2 = male;
/* example 2 */
enum fruits
{
apple, // 3
banana = 4, // 4
orange // 5
};
}
004. Write a program to accept values of two numbers and print their multiplication in C language
#include <iostream>
using namespace std;
int i;
int print_extern();
int main() {
i = 10;
print_extern();
}
005. Extern
#include <iostream>
using namespace std;
extern int i;
void print_extern()
{
cout << i << endl;
}
#include <iostream>
using namespace std;
class vehicle{
public:
int total_wheels;
float mileage;
};
vehicle v1,v2;
v1.total_wheels = 4;
[Link] = 110;
v2.total_wheels = 2;
[Link] = 80;
cout << v1.total_wheels << " wheeler with " << [Link] << " mileage" << endl;
cout << v2.total_wheels << " wheeler with " << [Link] << " mileage" << endl;
return 0;
}
007. Scope resolution operator
#include<iostream>
using namespace std;
int x = 10;
int main()
{
int x = 20;
cout << "Global x = " << ::x;
cout << "\nLocal x = " << x;
return 0;
}
#include<iostream>
using namespace std;
class X
{
public:
void foo()
{
cout << "Hello from foo()";
}
};
int main()
{
X a;
[Link]();
return 0;
}
#include<iostream>
using namespace std;
class X
{
public:
void foo();
};
void X::foo()
{
cout << "Hello from foo()";
}
int main()
{
X a;
[Link]();
return 0;
}
#include<iostream>
using namespace std;
class Maths{
public:
int a, b;
cout << "Addition of " << m1.a << " and "
<< m1.b << " is.. "
<< [Link](m1.a, m1.b);
return 0;
}
#include <iostream>
using namespace std;
class Maths
{
public:
int a,b;
int add(int a, int b);
};
int main()
{
Maths m1;
m1.a = 10;
m1.b = 20;
cout << "Addition of " << m1.a << " and "
<< m1.b << " is "
<< [Link](m1.a, m1.b);
return 0;
}
#include <iostream>
using namespace std;
class Maths
{
private:
int c;
int add(int a, int b)
{
c = a + b;
}
public:
int a,b;
void show(int a, int b)
{
add(a, b);
cout << "Addition of " << a << " and "
<< b << " is " << c;
}
};
int main()
{
Maths m1;
m1.a = 10;
m1.b = 20;
[Link](m1.a, m1.b);
return 0;
}
#include <iostream>
using namespace std;
class ArrayDemo
{
int a[6];
public:
void getElements()
{
cout << "Enter 6 numbers in the array: " << endl;
for(int i=0; i<6;i++)
{
cin >> a[i];
}
}
void show()
{
cout << "\nArray elements are " << endl;
for(int i=0; i<6;i++)
{
cout << a[i] << endl;
}
}
};
int main()
{
ArrayDemo obj;
[Link]();
[Link]();
return 0;
}
#include<iostream>
using namespace std;
class Math
{
public:
void change(float);
};
int main()
{
Math m1;
float y = 3.5;
[Link](y);
#include<iostream>
using namespace std;
int main()
{
int x = 10;
int y = 20;
swap(x, y);
return 0;
}
#include<iostream>
using namespace std;
// Global variable
int num;
// Function declaration
int& show();
int& show()
{
return num;
}
int main()
{
show() = 5;
cout << "Output is: " << num;
return 0;
}
#include <iostream>
using namespace std;
return 0;
}
#include <iostream>
using namespace std;
class Math
{
private:
int weight;
public:
//friend function
friend int print(Math);
};
int print(Math m)
{
[Link] += 10;
return [Link];
}
int main()
{
Math m;
cout << "Weight: " << print(m);
return 0;
}
#include<iostream>
using namespace std;
class Maths
{
public:
int a, b, c;
int add(int a, int b);
int add(int a, int b, int c);
};
int main()
{
Maths m1;
m1.a = 10;
m1.b = 20;
m1.c = 30;
cout << "Addition of " << m1.a << " and "
<< m1.b << " is "
<< [Link](m1.a, m1.b);
cout << "\nAddition of " << m1.a << " and "
<< m1.b << " and " << m1.c << " is "
<< [Link](m1.a, m1.b, m1.c);
return 0;
}
#include <iostream>
using namespace std;
class Numbers
{
int a,b;
public:
// constructor declaration
Numbers(int, int);
void display(void)
{
count << "a = " << a
<< "\nb = " << b << endl;
}
};
int main()
{
// explicit call
Numbers n1 = Numbers(10, 20);
// implicit call
Numbers n2(20, 30);
return 0;
}
#include <iostream>
using namespace std;
class Numbers
{
int a,b;
public:
// inline constructor declaration
Numbers(int x, int y)
{
a = x;
b = y;
}
void display(void)
{
count << "a = " << a
<< "\nb = " << b << endl;
}
};
int main()
{
// explicit call
Numbers n1 = Numbers(10, 20);
return 0;
}
022. Multiple constructors
#include<iostream>
using namespace std;
class Numbers{
int a, b;
public:
Numbers(){ // no arguments
a = 0; b = 0;
}
void display(void){
cout << "a= " << a
<< "\nb= " << b << endl;
}
};
int main()
{
Numbers n1;
Numbers n2 = Numbers(10, 20);
return 0;
}
#include <iostream>
using namespace std;
class Numbers
{
int a,b;
public:
// constructor with default argument
Numbers(int x, int y = 2)
{
a = x;
b = y;
}
void display(void)
{
count << "a = " << a
<< "\nb = " << b << endl;
}
};
int main()
{
Numbers n1 = Numbers(10);
return 0;
}
#include <iostream>
using namespace std;
class Numbers
{
int a,b;
public:
Numbers()
{
a = 0;
b = 0;
}
Numbers(int x, int y = 2)
{
a = x;
b = y;
}
Numbers(Numbers &i)
{
a = i.a;
b = i.b;
}
void display(void)
{
count << "a = " << a
<< "\nb = " << b << endl;
}
};
int main()
{
Numbers n1(10, 20);
Numbers n2(n1);
return 0;
}
025. Destructors
#include<iostream>
using namespace std;
class Text
{
int a, b;
public:
Text()
{
cout << "Constructor called" << endl;
}
~Text()
{
cout << "Destructor called" << endl;
}
};
int main()
{
Text t1;
return 0;
}
#include <iostream>
using namespace std;
class Math{
public:
int a;
};
int main()
{
int Math::*pA = &Math::a;
Math m1;
m1.a = 1; // direct access
cout << "Value of a is " << m1.a << endl;
m1.*pA = 2; // access via pointer to member
cout << "Value of a is " << m1.a << endl;
return 0;
}
#include <iostream>
using namespace std;
class Math{
public:
int a;
void add()
{
cout << "add()\n";
}
};
int main(){
Math m1;
Math *pM1 = new Math;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int *x;
x = new int[10];
if(x == NULL)
cout << "Memory is not allocated";
else
cout << "Memory is allocated";
delete x;
return 0;
}
029. Manipulators
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int a, b;
a = 100;
b = 200;
return 0;
}
#include <iostream>
using namespace std;
class Math {
public:
int a, b, c;
void operator+(){
a = ++a;
b = ++b;
}
void operator-(){
a = --a;
b = --b;
}
void display(){
cout << "a= " << a << "\tb= " << b << endl;
}
};
int main()
{
Math m1;
m1.a = 10;
m1.b = 20;
+m1;
cout << "Increment number\n";
[Link]();
-m1;
cout << "Decrement number\n";
[Link]();
return 0;
}
#include <iostream>
using namespace std;
class Math {
public:
int a, b, c;
Math(){
this->a = 0;
this->b = 0;
}
int main()
{
Math m1(10, 20);
Math m2(20, 30);
Math m3;
m3 = m1+ m2;
cout << "Total a & b: " << m3.a
<< " & " << m3.b;
return 0;
}
#include <iostream>
using namespace std;
class Maths{
int a;
public:
int b;
void get_val();
int get_a(void);
};
int main(){
addition a1;
a1.get_val();
[Link]();
[Link]();
a1.b=20;
[Link]();
[Link]();
return 0;
}
#include <iostream>
using namespace std;
class Vehicle{
protected:
int int_id;
public:
int b;
void get_id(int);
int put_id(void);
};
int main(){
Show s1;
s1.get_id(1);
s1.get_version(1.3);
[Link]();
return 0;
}
#include <iostream>
using namespace std;
class A{
protected:
int a;
public:
void get_a(int);
};
class B{
protected:
int b;
public:
void get_b(int);
};
void A :: get_a(int x)
{
a = x;
}
void C :: display(){
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << a + b << endl;
}
int main(){
C c1;
c1.get_a(15);
c1.get_b(16);
[Link]();
return 0;
}
#include <iostream>
using namespace std;
class Maths{
public:
int a, b;
void get_val(int, int);
};
int main(){
Addition a1;
Multiplication m1;
a1.get_val(15, 16);
[Link]();
m1.get_val(15, 16);
[Link]();
return 0;
}
#include <iostream>
using namespace std;
class Maths{
public:
int a;
};
class C{
public:
int b;
C(){
b = 10;
}
};
int main(){
Sum s1;
[Link]();
return 0;
}
#include<iostream>
using namespace std;
class Math
{
public:
int a, b;
void show()
{
cout << "a = " << a << "\n";
cout << "b = " << b << "\n";
}
};
};
int main()
{
D d1;
d1.get_a(10);
d1.get_b(20);
[Link]();
return 0;
}
038. Pointers
#include <iostream>
using namespace std;
int main() {
int *a, b;
b = 10;
cout << "Address of b (&b): "
<< &b << endl;
cout << "Value of b (b): "
<< b << endl << endl;
b = 20;
*a = 30;
cout << "Address of b (&b): "
<< &b << endl;
cout << "Value of b (b): "
<< b << endl << endl;
return 0;
}
#include<iostream>
using namespace std;
main()
{
//Array Declaration
int intArray[5] = {10,20,30,40,50};
int *ptr; //pointer point to int.
int sum = 0;
ptr = intArray;
return 0;
}
#include <iostream>
using namespace std;
int main () {
int intArray[5] = {10, 20, 30, 40, 50};
int *ptr[5];
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int cnt;
char str[8] = "kodegod";
char *ptr;
ptr = str;
cout << "\nValue of string in variable ptr : "
<< ptr;
#include <iostream>
using namespace std;
class Math {
private:
int a;
int b;
int c;
public:
Math(int x = 15, int y = 20, int z = 30) {
a = x;
b = y;
c = z;
}
int addition() {
return a + b + c;
}
};
int main() {
Math m1(30, 12, 15);
Math m2(5, 60, 20);
// Declare pointer to a class
Math *ptrMath;
return 0;
}
#include <iostream>
using namespace std;
class Math {
public:
private:
int a, b;
public:
Math(int x = 15, int y = 20) {
a = x; b = y;
}
int addition() {
return a + b;
}
int main(void) {
Math m1(5, 60);
Math m2(30, 12);
if([Link](m2)) {
cout << "m1 is greater than m2"
<<endl;
} else {
cout << "m1 is smaller than m2"
<<endl;
}
return 0;
}
#include <iostream>
using namespace std;
class Parent
{
public:
virtual void show()
{
cout << "This is Base class"
<< endl;
}
};
int main()
{
Parent* p; //pointer of base class
Child c; //object of derived class
p = &c;
p->show();
return 0;
}
#include <iostream>
using namespace std;
class Shape
{
public:
// pure virtual Function
virtual float calculateArea(float l) = 0;
};
int main()
{
Square s;
Circle c;
return 0;
}
046. istream
#include <iostream>
using namespace std;
int main()
{
char x;
[Link](x);
cout << x;
}
047. ostream
#include <iostream>
using namespace std;
int main()
{
char x;
[Link](x);
048. iostream
#include <iostream>
using namespace std;
int main()
{
// this function display
// ncount character from array
[Link]("kodegod", 5);
}
#include <iostream>
using namespace std;
class Stream {
public:
int x, y;
int main()
{
Stream s;
cout << "Enter two numbers x and y\n";
s >> cin;
cout << "x = " << s.x << "\ty = " << s.y;
}
#include <iostream>
using namespace std;
class Stream {
public:
int x, y;
Stream()
{
x = 10; y = 20;
}
// using friend function
friend void operator<< (Stream& s, ostream& scout)
{
// cout assigned to another object scout
scout << "Value of x and y are \n";
scout << s.x << " " << s.y;
}
};
int main()
{
Stream s;
s << cout;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int size = 10;
char country[10];
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout << "Kode";
[Link](5);
cout << "God";
[Link](10);
cout << ".com";
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
[Link](3);
cout << "Square root of 54:\n" << sqrt(54);
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
[Link]('*');
cout << "Kode";
[Link](5);
cout << "God";
[Link](10);
cout << ".com";
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
[Link]('*');
[Link](ios::left, ios::adjustfield);
[Link](15);
cout << "[Link]";
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
[Link]('*');
[Link](2);
[Link](20);
[Link](ios::showpoint);
[Link](ios::showpos);
[Link](ios::scientific, ios::floatfield);
cout << "Square root of 54:\n";
[Link](10);
cout << sqrt(54);
}
057. Formatted console unsetf
#include <iostream>
using namespace std;
int main () {
[Link](ios::hex, ios::basefield);
[Link](ios::showbase);
cout << 80 << '\n';
[Link] (ios::showbase);
cout << 80 << '\n';
return 0;
}
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
[Link](ios::showpoint);
cout << setfill('#')
<< setw(4) << "k"
<< setw(25) << "Square root of 54: "
<< setprecision(3) << sqrt(54)
<< setw(10) << "Kodegod\n";
return 0;
}
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
cout << "kode" << sign << "God" << sign << "com";
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
// Creation of ofstream class object
ofstream fout;
string line;
// Press -1 to exit
if (line == "-1")
break;
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int N = 100;
char line[N];
ofstream fout;
[Link]("[Link]");
fout << "Kodegod";
[Link]();
[Link]("[Link]");
fout << "[Link]";
[Link]();
ifstream fin;
[Link]("[Link]");
cout << "Text in file [Link]:\n";
while(fin)
{
[Link](line, N);
cout << line << endl;
}
[Link]();
[Link]("[Link]");
cout << "Text in file [Link]:\n";
while(fin)
{
[Link](line, N);
cout << line << endl;
}
[Link]();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int N = 100;
char line[N];
[Link]("[Link]");
[Link]("[Link]");
if([Link]() != 0)
{
cout << "Exit from [Link]" << endl;
exit(1);
}
[Link](line, N);
cout << "Text from [Link]: \n" << line << endl;
}
[Link]();
[Link]();
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
struct Employee {
int emp_id;
string name;
};
int main() {
ofstream fout("[Link]", ios::out | ios::binary);
if(!fout) {
cout << "Cannot open file!" << endl;
return 1;
}
Employee emp[2];
emp[0].emp_id = 1;
emp[0].name = "Prashant";
emp[1].emp_id = 2;
emp[1].name = "Pranit";
for(int i = 0; i < 2; i++)
[Link]((char *) &emp[i], sizeof(Employee));
[Link]();
Employee empr[2];
for(int i = 0; i < 2; i++)
[Link]((char *) &empr[i], sizeof(Employee));
[Link]();
cout<<"Employee Details:"<<endl;
for(int i=0; i < 2; i++) {
cout << "Emp Id: " << emp[i].emp_id << endl;
cout << "Name: " << emp[i].name << endl;
cout << endl;
}
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream fin;
[Link]("[Link]", ios::in);
while(![Link]())
{
// process the file
}
if([Link]())
{
// terminate the program
}
else if([Link]())
{
// report fatal error
}
else
{
[Link](); // clear error-state flags
}
}
#include <iostream>
using namespace std;
public:
Maths(T x, T y)
{
a = x;
b = y;
}
void display()
{
cout << "Numbers are: " << a
<< " and " << b << endl;
cout << "Addition is: "
<< add() << endl;
cout << "Subtraction is: "
<< subtract() << endl;
}
T add() { return a + b; }
T subtract() { return a - b; }
};
int main()
{
Maths<int> intMath(20, 10);
Maths<float> floatMath(18.8, 9.4);
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int x, y;
float m, n;
char c1, c2;
cout << "Enter two integers:\n";
cin >> x >> y;
cout << Compare(x, y) <<" is larger" << endl;
cout << "\nEnter two floating-point numbers:\n";
cin >> m >> n;
cout << Compare(m, n) <<" is larger" << endl;
cout << "\nEnter two characters:\n";
cin >> c1 >> c2;
cout << Compare(c1, c2) << " has larger ASCII value.";
return 0;
}
// function template
template <class T> T display(T a, T b)
{
cout << "Template values are : "
<< a << " and " << b << endl;
}
int main()
{
display(10, 20);
display(20, 10);
display('P', 'K');
return 0;
}
#include <iostream>
using namespace std;
int main() {
cout << addition<int, 10>(10) << '\n';
cout << addition<int, 20>(10) << '\n';
}
int main () {
int a = 10;
int b = 0;
double c = 0;
try {
c = division(a, b);
cout << c << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
}
#include <iostream>
using namespace std;
void ExcHandler()
{
try
{
throw "Thrown exception!";
}
catch (const char*)
{
cout << "Exception in function\n";
throw; //rethrow char* out of function
}
}
int main()
{
cout<< "Main function\n";
try
{
ExcHandler();
}
catch(const char*)
{
cout << "Exception in Main\n";
}
return 0;
}
#include<iostream>
using namespace std;
void ExcHandler(int a) {
try {
if (a > 0)
throw a;
else
throw 'a';
} catch (int x) {
cout << "Catch a integer: " << a << endl;
} catch (char a) {
cout << "Catch a character: " << a;
}
}
int main() {
cout << "Test multiple catch blocks:\n";
ExcHandler(1);
ExcHandler(0);
return 0;
}
void ExcHandler(int a)
throw(int, double, char){
if(a == 1)
throw 'a';
else if(a == 2)
throw a;
else if(a == -1)
throw 0;
}
int main() {
try {
cout << "a==1\n";
ExcHandler(1);
cout << "a==2\n";
ExcHandler(2);
cout << "a==-1\n";
ExcHandler(-1);
cout << "a==0\n";
ExcHandler(0);
} catch (char c) {
cout << "Character exception" << endl;
} catch (int a) {
cout << "Integer exception" << endl;
} catch (double d) {
cout << "Double exception" << endl;
}
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char str1[] = "Kode";
char str2[] = "God";
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main()
{
string str1 = "Kode";
return 0;
}
075. Iterators
#include<iostream>
#include<iterator>
#include<vector>
using namespace std;
int main()
{
vector<int> intArray = { 1, 2, 3, 4, 5 };
vector<int> intArray1 = {10, 20, 30};
return 0;
}
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> v1;
return 0;
}
#include <iostream>
#include <list>
using namespace std;
return 0;
}
#include <iostream>
#include <deque>
using namespace std;
int main()
{
deque <int> dq;
dq.push_back(10);
dq.push_front(20);
dq.push_back(30);
dq.push_front(40);
cout << "The deque is : ";
displaydq(dq);
return 0;
}
#include <iostream>
#include<set>
using namespace std;
int main(){
set<int> setOfNumbers;
[Link](1);
[Link](2);
[Link](3);
[Link](2);
set<int>::iterator it;
for (it = [Link](); it!=[Link](); ++it)
cout << ' ' << *it;
cout<<"\n";
return 0;
}
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int, greater<int>> msSet;
[Link](10);
[Link](20);
[Link](30);
[Link](20);
[Link](40);
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
map<int, string> Students;
Students[101] = "Nikhil";
Students[102] = "Alex";
Students[103] = "John";
Students[104] = "Neel";
Students[105] = "Sunny";
map<int,string>::iterator it;
for( it=[Link](); it!=[Link](); ++it)
{
cout << (*it).first << ": " << (*it).second << endl;
}
return 0;
}
int main()
{
multimap<string, string> mmCountry = {
{"India","Delhi"},
{"India", "Hyderabad"},
{"United Kingdom", "London"},
{"United States", "Texas"}
};
return 0;
}
#include <iostream>
#include <stack>
using namespace std;
int main ()
{
stack <int> st;
[Link](10);
[Link](20);
[Link](30);
[Link](50);
[Link](40);
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue <int> q;
[Link](10);
[Link](20);
[Link](30);
cout << "The queue is : ";
displayQueue(q);
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
priority_queue <int> pq;
[Link](10);
[Link](30);
[Link](20);
[Link](50);
[Link](40);
return 0;
}
#include <iostream>
using namespace std;
class Test{
private:
mutable int a;
public:
explicit Test(int x = 0){
a = x;
}
void change() const{
a = a + 15;
}
int display() const{
return a;
}
};
int main(){
const Test t(200);
cout << "Value of a: " << [Link]();
cout << "\n";
[Link]();
cout << "Value of a: " << [Link]();
return 0;
}