Documente Academic
Documente Profesional
Documente Cultură
Output:
Output:
Output:
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:
#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:
{
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:
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;
}
};
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)
Output:
#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:
#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:
#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();
}
Output
Displaying address using arrays:
&a[0] = 0x7fff5fbff880
&a[1] = 0x7fff5fbff884
&a[2] = 0x7fff5fbff888
&a[3] = 0x7fff5fbff88c
&a[4] = 0x7fff5fbff890
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
Output:
Enter
1
Enter
1
Enter
2
Enter
3
Enter
5
Enter
8
Enter
13
Enter
21
Enter
34
Enter
55
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
Output:
#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:
#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: