All CPP Programs
All CPP Programs
#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;
v1.mileage = 110;
v2.total_wheels = 2;
v2.mileage = 80;
cout << v1.total_wheels << " wheeler with " << v1.mileage << " mileage" << endl;
cout << v2.total_wheels << " wheeler with " << v2.mileage << " 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;
a.foo();
return 0;
}
#include<iostream>
using namespace std;
class X
{
public:
void foo();
};
void X::foo()
{
cout << "Hello from foo()";
}
int main()
{
X a;
a.foo();
return 0;
}
#include<iostream>
using namespace std;
class Maths{
public:
int a, b;
cout << "Addition of " << m1.a << " and "
<< m1.b << " is.. "
<< m1.add(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 "
<< m1.add(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;
m1.show(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;
obj.getElements();
obj.show();
return 0;
}
#include<iostream>
using namespace std;
class Math
{
public:
void change(float);
};
int main()
{
Math m1;
float y = 3.5;
m1.change(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)
{
m.weight += 10;
return m.weight;
}
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 "
<< m1.add(m1.a, m1.b);
cout << "\nAddition of " << m1.a << " and "
<< m1.b << " and " << m1.c << " is "
<< m1.add(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";
m1.display();
-m1;
cout << "Decrement number\n";
m1.display();
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();
a1.add();
a1.display();
a1.b=20;
a1.add();
a1.display();
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);
s1.display();
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);
c1.display();
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);
a1.add();
m1.get_val(15, 16);
m1.mul();
return 0;
}
#include <iostream>
using namespace std;
class Maths{
public:
int a;
};
class C{
public:
int b;
C(){
b = 10;
}
};
int main(){
Sum s1;
s1.sum();
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);
d1.show();
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(m1.compare(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;
cin.get(x);
cout << x;
}
047. ostream
#include <iostream>
using namespace std;
int main()
{
char x;
cin.get(x);
048. iostream
#include <iostream>
using namespace std;
int main()
{
// this function display
// ncount character from array
cout.write("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";
cout.width(5);
cout << "God";
cout.width(10);
cout << ".com";
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout.precision(3);
cout << "Square root of 54:\n" << sqrt(54);
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout.fill('*');
cout << "Kode";
cout.width(5);
cout << "God";
cout.width(10);
cout << ".com";
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout.fill('*');
cout.setf(ios::left, ios::adjustfield);
cout.width(15);
cout << "KodeGod.com";
}
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
cout.fill('*');
cout.precision(2);
cout.width(20);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.setf(ios::scientific, ios::floatfield);
cout << "Square root of 54:\n";
cout.width(10);
cout << sqrt(54);
}
057. Formatted console unsetf
#include <iostream>
using namespace std;
int main () {
cout.setf(ios::hex, ios::basefield);
cout.setf(ios::showbase);
cout << 80 << '\n';
cout.unsetf (ios::showbase);
cout << 80 << '\n';
return 0;
}
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
int main()
{
cout.setf(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;
fout.open("kode.txt");
fout << "Kodegod";
fout.close();
fout.open("kodegod.txt");
fout << "Kodegod.com";
fout.close();
ifstream fin;
fin.open("kode.txt");
cout << "Text in file kode.txt:\n";
while(fin)
{
fin.getline(line, N);
cout << line << endl;
}
fin.close();
fin.open("kodegod.txt");
cout << "Text in file kodegod.txt:\n";
while(fin)
{
fin.getline(line, N);
cout << line << endl;
}
fin.close();
return 0;
}
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
const int N = 100;
char line[N];
fin1.open("kode.txt");
fin2.open("kodegod.txt");
if(fin2.eof() != 0)
{
cout << "Exit from kodegod.txt" << endl;
exit(1);
}
fin2.getline(line, N);
cout << "Text from kodegod.txt: \n" << line << endl;
}
fin1.close();
fin2.close();
return 0;
}
#include<iostream>
#include<fstream>
using namespace std;
struct Employee {
int emp_id;
string name;
};
int main() {
ofstream fout("employee.dat", 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++)
fout.write((char *) &emp[i], sizeof(Employee));
fout.close();
Employee empr[2];
for(int i = 0; i < 2; i++)
fin.read((char *) &empr[i], sizeof(Employee));
fin.close();
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;
fin.open("test.txt", ios::in);
while(!fin.fail())
{
// process the file
}
if(fin.eof())
{
// terminate the program
}
else if(fin.bad())
{
// report fatal error
}
else
{
fin.clear(); // 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;
setOfNumbers.insert(1);
setOfNumbers.insert(2);
setOfNumbers.insert(3);
setOfNumbers.insert(2);
set<int>::iterator it;
for (it = setOfNumbers.begin(); it!=setOfNumbers.end(); ++it)
cout << ' ' << *it;
cout<<"\n";
return 0;
}
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int, greater<int>> msSet;
msSet.insert(10);
msSet.insert(20);
msSet.insert(30);
msSet.insert(20);
msSet.insert(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=Students.begin(); it!=Students.end(); ++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;
st.push(10);
st.push(20);
st.push(30);
st.push(50);
st.push(40);
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main()
{
queue <int> q;
q.push(10);
q.push(20);
q.push(30);
cout << "The queue is : ";
displayQueue(q);
return 0;
}
#include <iostream>
#include <queue>
using namespace std;
int main ()
{
priority_queue <int> pq;
pq.push(10);
pq.push(30);
pq.push(20);
pq.push(50);
pq.push(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: " << t.display();
cout << "\n";
t.change();
cout << "Value of a: " << t.display();
return 0;
}