C++ Programs
C++ Programs
#include <iostream>
int main() {
while (decimal != 0)
remainder = decimal % 2;
decimal = decimal / 2;
product *= 10;
cout << "The number in the binary form is: " << binary ;
return 0 ;
}
2. PROGRAM TO INSERT AN ELEMENT IN ARRAY.
#include<iostream>
int main()
cin>>tot;
cin>>arr[i];
cin>>elem;
cin>>pos;
arr[i] = arr[i-1];
arr[i] = elem;
tot++;
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}
3. PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER.
#include <iostream>
#include <bits/stdc++.h>
int main()
int n;
int a[n];
cin>>a[i];
{ if(a[i]>a[j])
a[i] = a[j];
a[j] = temp;
} } }
cout<<a[i]<<" ";
return 0;
}
4. PROGRAM WITH CLASS.
#include <iostream>
#include<conio.h>
class person {
public:
string name;
int number;
};
int main() {
person obj;
cout << obj.name << ": " << obj.number << endl;
getch();
return 0;
}
5. PROGRAM WITH CONSTRUCTOR.
#include<iostream>
class student
int rno;
char name[50];
double fee;
public:
student()
cin>>rno;
cin>>name;
cin>>fee;
void display()
{ cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}};
int main()
{ student s;
s.display();
return 0
}
6. PROGRAM OF COPY CONSTRUCTOR.
#include<iostream>
class Demo {
private:
public:
num1 = n1;
num2 = n2;}
num1 = n.num1;
num2 = n.num2; }
void display() {
} };
int main() {
obj1.display();
obj2.display();
return 0;
}
7. PROGRAM OF DESTRUCTOR.
#include <iostream>
class Test {
public:
};
main()
Test t;
return 0;
}
8. PROGRAM OF RECURSION.
#include <iostream>
int main() {
int n;
cin >> n;
if (n < 0)
else {
factorial *= i;
cout << "Factorial of " << n << " = " << factorial;
return 0;
}
9. PROGRAM OF SCOPE RESOLUTION OPERATOR.
#include<iostream>
int x;
int main()
int x = 10;
return 0;
}
10. PROGRAM OF STATIC CLASS.
#include <iostream>
class Example {
public :
static int a;
return 0;
};
int Example::a=28;
int main() {
Example obj;
Example::func(8);
cout << "\nThe value of the static data member a is: " << obj.a;
return 0;
}
11. PROGRAM OF VIRTUAL BASE CLASS.
#include <iostream>
class A {
public:
int a;
A()
a = 10;
};
};
};
};
int main()
D object;
return 0;
}
12. PROGRAM OF VIRTUAL CLASS.
#include <iostream>
class Base {
public:
};
public:
void print() {
};
int main() {
Derived derived1;
base1->print();
return 0;
}
13. PROGRAM OF STRING CONCATENATION.
#include <iostream>
int main()
init.append(add);
return 0;
}
14. PROGRAM OF ‘THIS’ OPERATOR.
#include<iostream>
class Test
private:
int x;
public:
this->x = x;
};
int main()
Test obj;
int x = 20;
obj.setX(x);
obj.print();
return 0;
}
15. Program to demonstrate the working of friend function.
#include <iostream>
class Distance {
private:
int meter;
public:
Distance() : meter(0) {}
};
int addFive(Distance d) {
d.meter += 5;
return d.meter;
int main() {
Distance D;
return 0;
}
16. PROGRAM OF NEW OPERATOR.
#include <iostream>
using namespace std;
int main () {
int *ptr1 = NULL;
ptr1 = new int;
float *ptr2 = new float(299.121);
int *ptr3 = new int[28];
*ptr1 = 28;
cout << "Value of pointer variable 1 : " << *ptr1 << endl;
cout << "Value of pointer variable 2 : " << *ptr2 << endl;
if (!ptr3)
cout << "Allocation of memory failed\n";
else {
for (int i = 10; i < 15; i++)
ptr3[i] = i+1;
cout << "Value of store in block of memory: ";
for (int i = 10; i < 15; i++)
cout << ptr3[i] << " ";
}
delete ptr1;
delete ptr2;
delete[] ptr3;
return 0;
}
17. PROGRAM OF FUNCTION OVERLOADING.
#include <iostream>
int main()
add(10, 2);
add(5.3, 6.2);
return 0;
}
18. PROGRAM OF OPERATOR OVERLOADING.
#include <iostream>
class Complex {
private:
public:
Complex(int r = 0, int i = 0)
real = r;
imag = i;
{ Complex res;
return res;
void print() { cout << real << " + i" << imag << '\n'; }
};
int main()
Complex c3 = c1 + c2;
c3.print();
}
19. PROGRAM OF MULTILEVEL INHERITANCE.
#include<iostream>
class A
public:
};
class B
public:
};
public:
int main()
C c;
return 0;
}
20. PROGRAM OF EXCEPTION HANDLING.
#include <iostream>
int main()
int x = -1;
try {
if (x < 0)
throw x;
catch (int x ) {
return 0;
}
21. PROGRAM OF HYBRID INHERITANCE.
#include <iostream>
using namespace std;
class Animals // indicates class A
{
public:
Animals()
{ cout<< "This is an animal\n";
} };
class Mammals: public Animals
{ public:
Mammals()
{ cout<< "This is a mammal\n";
} };
class Herbivores
{
public:
Herbivores()
{ cout<< "This is a herbivore\n";
}
};
class Cow: public Mammals, public Herbivores
{
public:
Cow()
{
cout<< "A cow is a herbivore mammal\n";
}
};
int main() {
Cow c;
return 0;
}
22. PROGRAM OF OVERLOADING A FUNCTION TEMPLATE.
#include <bits/stdc++.h>
int main()
display(200);
display(12.40);
display('G');
return 0;