Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

C++ Programs

The document contains 22 C++ programs demonstrating various OOP concepts: 1. A program to convert a decimal number to binary. 2. A program to insert an element into an array. 3. A program to sort an array in ascending order. The programs demonstrate concepts like classes, constructors, destructors, inheritance, polymorphism, operator overloading, function overloading, templates and exception handling.

Uploaded by

muskan11825
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

C++ Programs

The document contains 22 C++ programs demonstrating various OOP concepts: 1. A program to convert a decimal number to binary. 2. A program to insert an element into an array. 3. A program to sort an array in ascending order. The programs demonstrate concepts like classes, constructors, destructors, inheritance, polymorphism, operator overloading, function overloading, templates and exception handling.

Uploaded by

muskan11825
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 44

1.

PROGRAM TO CONVERT DECIMAL NUMBER INTO BINARY NUMBER

#include <iostream>

using namespace std;

int main() {

int decimal, binary = 0, remainder, product = 1;

cin >> decimal;

while (decimal != 0)

remainder = decimal % 2;

binary = binary + (remainder * product);

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>

using namespace std;

int main()

int arr[50], i, elem, pos, tot;

cout<<" Enter the Size for Array: ";

cin>>tot;

cout<<" Enter "<<tot<<" Array Elements: ";

for(i=0; i<tot; i++)

cin>>arr[i];

cout<<" Enter Element to Insert: ";

cin>>elem;

cout<<"At What Position ? ";

cin>>pos;

for(i=tot; i>=pos; i--)

arr[i] = arr[i-1];

arr[i] = elem;

tot++;

cout<<" The New Array is: ";

for(i=0; i<tot; i++)

cout<<arr[i]<<" ";

cout<<endl;

return 0;

}
3. PROGRAM TO SORT AN ARRAY IN ASCENDING ORDER.

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main()

int n;

cout<<" Enter the size of array: "; cin>>n;

int a[n];

cout<<" Enter the elements: ";

for(int i=0; i<n; i++)

cin>>a[i];

for(int i=0; i<n; i++)

{ for(int j=i+1; j<n; j++)

{ if(a[i]>a[j])

{ int temp = a[i];

a[i] = a[j];

a[j] = temp;

} } }

cout<< " Array after swapping: ";

for(int i=0; i<n; i++)

cout<<a[i]<<" ";

return 0;

}
4. PROGRAM WITH CLASS.

#include <iostream>

#include<conio.h>

using namespace std;

class person {

public:

string name;

int number;

};

int main() {

person obj;

cout << "Enter the Name :";

cin >> obj.name;

cout << "Enter the Number :";

cin >> obj.number;

cout << obj.name << ": " << obj.number << endl;

getch();

return 0;

}
5. PROGRAM WITH CONSTRUCTOR.

#include<iostream>

using namespace std;

class student

int rno;

char name[50];

double fee;

public:

student()

{ cout<<"Enter the RollNo:";

cin>>rno;

cout<<"Enter the Name:";

cin>>name;

cout<<"Enter the Fee:";

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>

using namespace std;

class Demo {

private:

int num1, num2;

public:

Demo(int n1, int n2) {

num1 = n1;

num2 = n2;}

Demo(const Demo &n) {

num1 = n.num1;

num2 = n.num2; }

void display() {

cout<<"num1 = "<< num1 <<endl;

cout<<"num2 = "<< num2 <<endl;

} };

int main() {

Demo obj1(10, 20);

Demo obj2 = obj1;

obj1.display();

obj2.display();

return 0;

}
7. PROGRAM OF DESTRUCTOR.

#include <iostream>

using namespace std;

class Test {

public:

Test() { cout << "\n Constructor executed"; }

~Test() { cout << "\nDestructor executed"; }

};

main()

Test t;

return 0;

}
8. PROGRAM OF RECURSION.

#include <iostream>

using namespace std;

int main() {

int n;

long factorial = 1.0;

cout << "Enter a positive integer: ";

cin >> n;

if (n < 0)

cout << "Error! Factorial of a negative number doesn't exist.";

else {

for(int i = 1; i <= n; ++i) {

factorial *= i;

cout << "Factorial of " << n << " = " << factorial;

return 0;

}
9. PROGRAM OF SCOPE RESOLUTION OPERATOR.

#include<iostream>

using namespace std;

int x;

int main()

int x = 10;

cout << "Value of global x is " << ::x;

cout << "\nValue of local x is " << x;

return 0;

}
10. PROGRAM OF STATIC CLASS.

#include <iostream>

using namespace std;

class Example {

public :

static int a;

static int func(int b) {

cout << "Static member function called";

cout << "\nThe value of b is: " << b;

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>

using namespace std;

class A {

public:

int a;

A()

a = 10;

};

class B : public virtual A {

};

class C : public virtual A {

};

class D : public B, public C {

};

int main()

D object;

cout << "a = " << object.a << endl;

return 0;
}
12. PROGRAM OF VIRTUAL CLASS.

#include <iostream>

using namespace std;

class Base {

public:

virtual void print() {

cout << "Base Function" << endl;

};

class Derived : public Base {

public:

void print() {

cout << "Derived Function" << endl;

};

int main() {

Derived derived1;

Base* base1 = &derived1;

base1->print();

return 0;

}
13. PROGRAM OF STRING CONCATENATION.

#include <iostream>

using namespace std;

int main()

string init("this is in it");

string add(" added now");

init.append(add);

cout << init << endl;

return 0;

}
14. PROGRAM OF ‘THIS’ OPERATOR.

#include<iostream>

using namespace std;

class Test

private:

int x;

public:

void setX (int x)

this->x = x;

void print() { cout << "x = " << x << endl; }

};

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>

using namespace std;

class Distance {

private:

int meter;

friend int addFive(Distance);

public:

Distance() : meter(0) {}

};

int addFive(Distance d) {

d.meter += 5;

return d.meter;

int main() {

Distance D;

cout << "Distance: " << addFive(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>

using namespace std;

void add(int a, int b)

cout << "sum = " << (a + b);

void add(double a, double b)

cout << endl << "sum = " << (a + b);

int main()

add(10, 2);

add(5.3, 6.2);

return 0;

}
18. PROGRAM OF OPERATOR OVERLOADING.

#include <iostream>

using namespace std;

class Complex {

private:

int real, imag;

public:

Complex(int r = 0, int i = 0)

real = r;

imag = i;

Complex operator+(Complex const& obj)

{ Complex res;

res.real = real + obj.real;

res.imag = imag + obj.imag;

return res;

void print() { cout << real << " + i" << imag << '\n'; }

};

int main()

{ Complex c1(10, 5), c2(2, 4);

Complex c3 = c1 + c2;

c3.print();
}
19. PROGRAM OF MULTILEVEL INHERITANCE.

#include<iostream>

using namespace std;

class A

public:

A() { cout << "A's constructor called" << endl; }

};

class B

public:

B() { cout << "B's constructor called" << endl; }

};

class C: public B, public A // Note the order

public:

C() { cout << "C's constructor called" << endl; }

int main()

C c;

return 0;

}
20. PROGRAM OF EXCEPTION HANDLING.

#include <iostream>

using namespace std;

int main()

int x = -1;

cout << "Before try \n";

try {

cout << "Inside try \n";

if (x < 0)

throw x;

cout << "After throw (Never executed) \n";

catch (int x ) {

cout << "Exception Caught \n";

cout << "After catch (Will be executed) \n";

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>

using namespace std;

template <class T>

void display(T t1)

cout << "Displaying Template: "

<< t1 << "\n";

void display(int t1)

cout << "Explicitly display: "

<< t1 << "\n";

int main()

display(200);

display(12.40);

display('G');

return 0;

You might also like