Sunteți pe pagina 1din 27

(Constructors & Destructors)

C++ Program to add two numbers by constructor.


#include<iostream>
using namespace std;
class sum
{
int a,b;
public:
sum()
{
cout<< "Enter 2 numbers" << endl;
cin>>a>>b;
cout< "Sum = "<a+b;
}
~sum()
{
cout< "\nDestructor invoked"<endl;
}
};
int main()
{
sum t;
}

Output:

C++ Program to calculate area & volume by


constructor
#include<iostream>
using namespace std;
class av
{
int a,b,l;
public:
av()
{
cout<< "Enter length & breadth" << endl;
cin>>b;
a=l*b;
cout<< "Area = "<<a<<endl;
}
av(int s)
{
int v;
v=s*s*s;
cout<< "Volume = "<<v<<endl;
}
~av()
{
cout<< "Destructor invoked"<<endl;
}
};
int main()
{
av t;
int s;
cout<< "enter side"<<endl;
cin>>s;
av t1(s);
}

Output:

C++ Program to add two values of time by constructor.


#include<iostream>
using namespace std;
class time
{
int mins,sec;
public:
time()
{
cout<< "Enter minutes and seconds" <<endl;
cin>>mins>>sec;
}
time(time &t1,time &t2)
{
mins=(t1.mins+t2.mins)+(t1.sec+t2.sec)/60;
sec=(t1.sec+t2.sec)%60;
cout<< "Minutes = "<<mins<<endl;
cout<< "Seconds = "<<sec<<endl;
}
~time()
{
cout<< "Destructor invoked"<<endl;
}
};
int main()
{
time o1,o2,o3(o1,o2);
}

Output:

C++ Program to concatenate two strings by constructor


#include<iostream>
#include<string.h>
using namespace std;
class String
{
char *name;
int length;
public:
String()
{
length=0;
name= new char[length+1];
}
String(char *s)
{
length=strlen(s);
name= new char[length+1];
strcpy(name,s);
}
void display()
{
cout<<name<<endl;
}
void join(String &a,String &b)
{
length=a.length+b.length;
delete name;
name= new char[length+1];
strcpy(name,a.name);
strcat(name,b.name);
}
};
int main()
{
String name1("cprogram"),name2("bank"),s1;
s1.join(name1,name2);
name1.display();
name2.display();
s1.display();
}

Output:

(Inheritence)
C++ Program to input & Display College and teacher name
#include<iostream>
using namespace std;
class college
{
char n[10];
public:
void st()
{
cout<< "Enter college name"<<endl;
cin>>n;
}
void ts()
{
cout<< "College = "<<n<<endl;
}
};
class teacher:public college
{
char n[10];
public:
void st1()
{
st();
cout<< "Enter teacher name"<<endl;
cin>>n;
}
void ts1()
{
ts();
cout<< "Teacher = "<<n<<endl;
}
};
int main()
{
teacher t;
t.st1();
t.ts1();
}

Output:

C++ Program to input details and marks of student.

#include<iostream>
using namespace std;
class student
{
protected:
int roll;
public:
void get_number(int);
void put_number(void);
};
void student::get_number(int a)
{
roll=a;
}
void student::put_number()
{
cout<< "Roll No. = "<<roll<<endl;
}
class test:public student
{
protected:
float sub1,sub2;
public:
void get_marks(float,float);
void put_marks(void);
};
void test ::get_marks(float x,float y)
{
sub1=x;
sub2=y;
}
void test::put_marks()
{
cout<< "Marks in Subject 1 : "<<sub1<<endl;
cout<< "Marks in Subject 2 : "<<sub2<<endl;
}
class result:public test
{
float total;
public:
void display();
};
void result::display()
{
total=sub1+sub2;
put_number();
put_marks();
cout<< "Total = "<<total<<endl;
}

int main()
{
result student1;
student1.get_number(111);
student1.get_marks(75.0,67.5);
student1.display();
}

Output:

C++ Program to multiply 2 numbers


#include<iostream>
using namespace std;
class first

{
protected:
int a;
public:
void geta(int);
};
class second
{
protected:
int b;
public:
void getb(int);
};
class product:public first,public second
{
public:
void display();
};
void first::geta(int x)
{
a=x;
}
void second::getb(int y)
{
b=y;
}
void product::display()
{
cout<< "a = "<<a<<endl;
cout<< "b = "<<b<<endl;
cout<< "a*b = "<<a*b<<endl;
}
int main()
{
product p;
p.geta(10);
p.getb(20);
p.display();
}

Output:

C++ Program to add marks of student.


#include<iostream>
using namespace std;

class student
{
char n[10];
public:
void st()
{
cout<< "Enter name"<<endl;
cin>>n;
}
void ts()
{
cout << "Name = "<<n<<endl;
}
};
class internal:public student
{
protected:
int m1;
public:
void st1()
{
st();
cout<< "Enter internal marks"<<endl;
cin>>m1;
}
void ts1()
{
ts();
cout<< "Internal marks = "<<m1<<endl;
}
};
class external
{
protected:
int m2;
public:
void st2()
{
cout<< "Enter external marks"<<endl;
cin>>m2;
}
void ts2()
{
cout<< "External marks = "<<m2<<endl;
}
};

class result:public internal,public external


{

public:
void st3()
{
st1();
st2();
}
void ts3()
{
ts1();
ts2();
cout<< "Final marks = "<<m1+m2<<endl;
}
};
int main()
{
result r;
r.st3();
r.ts3();
}

Output:

( FILE HANDLING)

C++ Program to read a file


#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream o("new.txt");
char ch;
while(o.eof()==0)
{
o.get(ch);
cout<<ch;
}
o.close();
}

Output:

C++ Program to copy contents from one file to


another.

#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ofstream o("1.txt");
ifstream ob("2.txt");
char ch;
while(ob.eof()==0)
{
ob.get(ch);
o.put(ch);
cout<<ch;
}
o.close();
ob.close();
}

Output:

( Classes & Objects )


C++ Program to find maximum marks among 5 students.

#include<iostream>
using namespace std;
class student
{
char n[10];
int r,m;
public:
void st()
{
cout<< "Enter name,roll & marks"<<endl;
cin>>n>>r>>m;
}
void ts()
{
cout<< " Name = " << n<<endl;
cout<< " Roll No. = " << r<<endl;
cout<< " Marks = " << m<<endl;
}
int marks()
{
return m;
}
};
int main()
{
student s[3];
int i,loc=-1,maximum=0;
for(i=0;i<=2;i++)
{
s[i].st();
}
for(i=0;i<=2;i++)
{
if(maximum<s[i].marks())
{
maximum=s[i].marks();
loc=i;
}
}
s[loc].ts();
}

Output:

C++ Program to add and substract two complex numbers


#include<iostream>

#include<conio.h>
using namespace std;
class complex
{
float real,imaginary;
public:
complex()
{
real=0.0;
imaginary=0.0;
}
complex(float r,float i)
{
real=r;
imaginary=i;
}
void enterdata()
{
cout<<"Enter real no."<<endl;
cin>>real;
cout<<"Enter imaginary no."<<endl;
cin>>imaginary;
}
void displaydata()
{
if(imaginary>=0)
cout<<"The complex no. is"<<real<<"+"
<<imaginary<<"i"<<endl;
else
cout<<"The complex no. is"<<real<<"-"
<<imaginary<<"i"<<endl;
}
void addcomp(complex c1,complex c2)
{
real=c1.real+c2.real;
imaginary=c1.imaginary+c2.imaginary;
}
void subcomp(complex c1,complex c2)
{
real=c2.real-c1.real;
imaginary=c2.imaginary-c1.imaginary;
}
};
int main()
{
complex com1,com2,com3,com4;
com1.enterdata();
Output:
com1.displaydata();
com2.enterdata();
com2.displaydata();
com3.addcomp(com1,com2);
com3.displaydata();
com4.subcomp(com1,com2);
com4.displaydata();
}

C++ Program to display address of elements of an array using both


array and pointers
#include <iostream>
using namespace std;
int main() {
float a[5];
float *ptr;
cout << "Displaying address using arrays: "<<endl;
for (int i = 0; i < 5; ++i) {
cout << "&a[" << i << "] = " << &a[i] <<endl;
}
ptr = a; // ptr = &a[0]
cout<<"\nDisplaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i) {
cout << "ptr + " << i << " = "<<ptr+i <<endl;
}
return 0;
}

Output
Displaying address using arrays:
&a[0] = 0x7fff5fbff880
&a[1] = 0x7fff5fbff884
&a[2] = 0x7fff5fbff888
&a[3] = 0x7fff5fbff88c
&a[4] = 0x7fff5fbff890

Displaying address using pointers:


ptr + 0 = 0x7fff5fbff880
ptr + 1 = 0x7fff5fbff884
ptr + 2 = 0x7fff5fbff888
ptr + 3 = 0x7fff5fbff88c
ptr + 4 = 0x7fff5fbff890

C++ Program to Perform Dictionary Operations in a Binary Search Tree


#include<iostream>
#include<conio.h>
#include<stdlib.h>
using namespace std;
# define max 10
typedef struct list
{
int data;
struct list *next;
} node_type;
node_type *ptr[max], *root[max], *temp[max];
class Dictionary
{
public:
int index;
Dictionary();
void insert(int);
void search(int);
void delete_ele(int);
};
Dictionary::Dictionary()
{
index = -1;
for (int i = 0; i < max; i++)
{
root[i] = NULL;
ptr[i] = NULL;
temp[i] = NULL;
}
}
void Dictionary::insert(int key)
{
index = int(key % max);
ptr[index] = (node_type*) malloc(sizeof(node_type));
ptr[index]->data = key;
if (root[index] == NULL)
{
root[index] = ptr[index];
root[index]->next = NULL;
temp[index] = ptr[index];
}
else
{
temp[index] = root[index];
while (temp[index]->next != NULL)
temp[index] = temp[index]->next;
temp[index]->next = ptr[index];
}
}
void Dictionary::search(int key)
{
int flag = 0;
index = int(key % max);
temp[index] = root[index];

while (temp[index] != NULL)


{
if (temp[index]->data == key)
{
cout << "\nSearch key is found!!";
flag = 1;
break;
}
else
temp[index] = temp[index]->next;
}
if (flag == 0)
cout << "\nsearch key not found.......";
}
void Dictionary::delete_ele(int key)
{
index = int(key % max);
temp[index] = root[index];
while (temp[index]->data != key && temp[index] != NULL)
{
ptr[index] = temp[index];
temp[index] = temp[index]->next;
}
ptr[index]->next = temp[index]->next;
cout << "\n" << temp[index]->data << " has been deleted.";
temp[index]->data = -1;
temp[index] = NULL;
free(temp[index]);
}
int main(int argc, char **argv)
{
int val, ch, n, num;
char c;
Dictionary d;
do
{
cout << "\nMENU:\n1.Create";
cout << "\n2.Search for a value\n3.Delete an value";
cout << "\nEnter your choice:";
cin >> ch;
switch (ch)
{
case 1:
cout << "\nEnter the number of elements to be inserted:";
cin >> n;
cout << "\nEnter the elements to be inserted:";
for (int i = 0; i < n; i++)
{
cin >> num;
d.insert(num);
}
break;
case 2:
cout << "\nEnter the element to be searched:";
cin >> n;
d.search(n);
case 3:
cout << "\nEnter the element to be deleted:";
cin >> n;
d.delete_ele(n);
break;
default:
cout << "\nInvalid choice....";

break;
}
cout << "\nEnter y to continue......";
cin >> c;
}
while (c == 'y');
}

Output:
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1
Enter the number of elements to be inserted:5
Enter the elements to be inserted:234 4563 0 2345 45
Enter y to continue......y
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
Enter the element to be searched: 0
Search key is found!!
Enter the element to be deleted:0
0 has been deleted.
Enter y to continue......y
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
Enter the element to be searched:234
Search key is found!!
Enter the element to be deleted:45
45 has been deleted.
Enter y to continue......n
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:1
Enter the number of elements to be inserted:5
Enter the elements to be inserted:234 4563 0 2345 45
Enter y to continue......y

MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
Enter the element to be searched: 0
Search key is found!!
Enter the element to be deleted:0
0 has been deleted.
Enter y to continue......y
MENU:
1.Create
2.Search for a value
3.Delete an value
Enter your choice:2
Enter the element to be searched:234
Search key is found!!
Enter the element to be deleted:45
45 has been deleted.
Enter y to continue......n
-----------------(program exited with code: 0)
Press return to continue

C++ Program to Find Fibonacci Numbers using Recursion


#include <cstring>
#include <iostream>
#include <cstdlib>
#define ll long long
using namespace std;
/*
* Recursive function to find Fibonnaci Numbers
*/
ll fibo_recur(int n)
{
if (n == 1 || n == 2)
return 1;
else
return fibo_recur(n - 1) + fibo_recur(n - 2);;
}
/*
* Main
*/
int main()
{
int n;
while (1)
{
cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";
cin>>n;
if (n == 0)
break;
cout<<fibo_recur(n)<<endl;
}
return 0;
}

Output:
Enter
1
Enter
1
Enter
2
Enter
3
Enter
5
Enter
8
Enter
13
Enter
21
Enter
34
Enter
55

the integer n to find nth fibonnaci no.(0 to exit): 1


the integer n to find nth fibonnaci no.(0 to exit): 2
the integer n to find nth fibonnaci no.(0 to exit): 3
the integer n to find nth fibonnaci no.(0 to exit): 4
the integer n to find nth fibonnaci no.(0 to exit): 5
the integer n to find nth fibonnaci no.(0 to exit): 6
the integer n to find nth fibonnaci no.(0 to exit): 7
the integer n to find nth fibonnaci no.(0 to exit): 8
the integer n to find nth fibonnaci no.(0 to exit): 9
the integer n to find nth fibonnaci no.(0 to exit): 10

C++ Program to find the sum of each row & column of a matrix of size n x m and if matrix is square,
find the sum of the diagonals also.
#include <iostream.h>
#include <conio.h>
int main()
{
clrscr();
int A[10][10],m,n,x,y,sum=0;
//Create a Matrix A
cout << "Enter number of rows and columns in Matrix A : \n";
cin>>n>>m;
cout << "Enter elements of Matrix A : \n";
for(x=1;x<n+1;++x)
for(y=1;y<m+1;++y)
cin>>A[x][y];
//Find sum of each row
for(x=1;x<n+1;++x)
{
A[x][m+1]=0;
for(y=1;y<m+1;++y)
A[x][m+1]=A[x][m+1]+A[x][y];
}
//Find sum of each column
for(y=1;y<m+1;++y)
{
A[n+1][y]=0;
for(x=1;x<n+1;++x)
A[n+1][y]+=A[x][y];
}
cout << "\nMatrix A, Row Sum (Last Column)" << " and Column Sum (Last Row) : \n";
for(x=1;x<n+1;++x)
{
for(y=1;y<m+2;++y)
cout << A[x][y] << "
";
cout << "\n";
}
//Print sum of each column
x=n+1;
for(y=1;y<m+1;++y)
cout << A[x][y] << "
";
cout << "\n";
if(m==n)
{
for(x=1;x<m+1;x++)
for(y=1;y<n+1;y++)
if(x==y)
sum+=A[x][y];
else
if(y==m-(x+1))
sum+=A[x][y];
}
cout << "Sum of diagonal elements is : " << sum << endl;
getch();
return 0;
}

Input:
33
987654321

SAMPLE OUTPUT
Matrix A, Row Sum (Last Column) and Column Sum (Last Row):
9
8
7
24
6
5
4
15
3
2
1
6
18
15
12
Sum of diagonal elements is: 15

Program to enter 10 integers in a single-dimension array and


then print out the array in ascending order
#include <iostream.h>
#include <conio.h>
void main()
{
clrscr();
int array[10],t;
for(int x=0;x<10;x++)
{
cout << "Enter Integer No. " << x+1 << " : " << endl;
cin>>array[x];
}
for (x=0;x<10;x++)
{
for(int y=0;y<9;y++)
{
if(array[y]>array[y+1])
{
t=array[y];
array[y]=array[y+1];
array[y+1]=t;
}
}
}
cout << "Array in ascending order is : ";
for (x=0;x<10;x++)
cout << endl << array[x];
getch();
}
Input:
43
67
53
21
6
78
92
48
95
8
Output:
Array in ascending order is :
6
8
21
43
48
53
67
78
92
95

C++ Program to calculate roots of quadratic equation.


#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
template <class T>
void roots(T a,T b,T c)
{
T d=b*b-4*a*c;
if(d==0)
{
cout<< "Roots are equal : "<<-b/(2*a)<<endl;
}
else if(d>0)
{
cout<< "Roots are real"<<endl;
float R=sqrt(d);
float R1=(-b+R)/(2*a);
float R2=(-b-R)/(2*a);
cout<< "R1 = "<<R1<<endl;
cout<< "R2 = "<<R2<<endl;
}
else
{
cout<< "Roots are complex"<<endl;
float R1=(-b)/(2*a);
float R2=sqrt(-d)/(2*a);
cout<< "Real Part = "<<R1<<endl;
cout<< "Imahinary Part = "<<R2<<endl;
}
}
int main()
{
int a,b,c;
float p,q,r;
cout<< "Enter Integer coefficients"<<endl;
cin>>a>>b>>c;
roots(a,b,c);
cout<< "Enter Float coefficients"<<endl;
cin>>p>>q>>r;
roots(p,q,r);
}

Output:

C++ Program to add marks of student. (hybrid inheritance)

#include<iostream>
using namespace std;
class student
{
char n[10];
public:
void st()
{
cout<< "Enter name"<<endl;
cin>>n;
}
void ts()
{
cout << "Name = "<<n<<endl;
}
};
class internal:public student
{
protected:
int m1;
public:
void st1()
{
st();
cout<< "Enter internal marks"<<endl;
cin>>m1;
}
void ts1()
{
ts();
cout<< "Internal marks = "<<m1<<endl;
}
};
class external
{
protected:
int m2;
public:
void st2()
{
cout<< "Enter external marks"<<endl;
cin>>m2;
}
void ts2()
{
cout<< "External marks = "<<m2<<endl;
}
};
class result:public internal,public external
{
public:
void st3()
{
st1();
st2();
}
void ts3()
{
ts1();
ts2();
cout<< "Final marks = "<<m1+m2<<endl;
}
};
int main()
{
result r;

r.st3();
r.ts3();

Output:

C++ Program to add two values of time. (operator overloading and


friend function)

#include<iostream>
using namespace std;
class time
{
int m,s;
public:
void st()
{
cout<< "Enter minutes & seconds"<<endl;
cin>>m>>s; }
void ts()
{
cout<<"Minutes = "<<m<<endl;
cout<<"Seconds = "<<s<<endl; }
friend time operator +(time,time);
};
time operator + (time t1,time t2)
{
time t3;
t3.m=(t1.m+t2.m)+(t1.s+t2.s)/60;
t3.s=(t1.s+t2.s)%60;
return t3;
}
int main()
{
time b1,b2,b3;
b1.st();
b2.st();
b3=b1+b2;
b3.ts();
}

Output:

S-ar putea să vă placă și