Multiple classes
If required we can have multiple classes in a single program. The classes may or may not be related.
Question:1
C++ program to declare a class named student with attributes as roll and name. Declare another class named physical with attributes as age, height and weight. Take input for the details and display them.
Sol:
#include <iostream>
#include<conio.h>
using namespace std;
class student
{
private:
int roll;
char name[20];
public:
void read();
void show();
};
class physical
{
private:
float age,ht,wt;
public:
void read();
void show();
};
void student::read()
{
cout<<"Enter roll and name ";
cin>>roll>>name;
}
void student::show()
{
cout<<"roll "<<roll<<" name "<<name<<endl;
}
void physical::read()
{
cout<<"Enter age, height and weight ";
cin>>age>>ht>>wt;
}
void physical::show()
{
cout<<"age "<<age<<" height "<<ht<<" weight "<<wt<<endl;
}
int main()
{
student s;
physical p;
s.read(); p.read();
s.show(); p.show();
return(0);
}
/* Output */ Enter roll and name 101 Amit Enter age, height and weight 10 5 3 roll 101 name Amit age 10 height 5 weight 3
Question:2
C++ program to declare a class named employee with attributes as empno and name. Declare another class named office with attributes as deptno, deptname and salary. Take input for the details and display them.
Sol:
#include<iostream>
#include<conio.h>
using namespace std;
class employee
{
private:
int empno;
char name[20];
public:
void read();
void show();
};
class office
{
private:
int dno;
char dname[20];
float sal;
public:
void read();
void show();
};
void employee::read()
{
cout<<"Enter empno and name ";
cin>>empno>>name;
}
void employee::show()
{
cout<<"Empno "<<empno<<" Name "<<name<<endl;
}
void office::read()
{
cout<<"Enter dno, dname and salary ";
cin>>dno>>dname>>sal;
}
void office::show()
{
cout<<"Dno "<<dno<<" Dname "<<dname<<" Salary "<<sal<<endl;
}
int main()
{
employee e;
office o;
e.read(); o.read();
e.show(); o.show();
return(0);
}
/* Output */ Enter empno and name 1001 Amit Enter dno, dname and salary 10 Sales 45000 Empno 1001 Name Amit Dno 10 Dname Sales Salary 45000
Question:3
C++ program to declare a class named student with attributes as roll and name. Declare another class named marks with attributes as m1,m2,m3,total and per. Take input for the details and display them.
Sol:
#include <iostream>
#include<conio.h>
using namespace std;
class student
{
private:
int roll;
char name[20];
public:
void read();
void show();
};
class marks
{
private:
float m1,m2,m3,total,per;
public:
void read();
void cal();
void show();
};
void student::read()
{
cout<<"Enter roll and name ";
cin>>roll>>name;
}
void student::show()
{
cout<<"roll "<<roll<<" name "<<name<<endl;
}
void marks::read()
{
cout<<"Enter m1,m2 and m3 ";
cin>>m1>>m2>>m3;
}
void marks::cal()
{
total=m1+m2+m3;
per=total/3;
}
void marks::show()
{
cal();
cout<<"m1 "<<m1<<" m2 "<<m2<<" m3 "<<m3<<endl;
cout<<"Total "<<total<<" Per "<<per<<endl;
}
int main()
{
student s;
marks m;
s.read(); m.read();
s.show(); m.show();
return(0);
}
/* Output */ Enter roll and name 1002 Sumit Enter m1,m2 and m3 95 96 93 roll 1002 name Sumit m1 95 m2 96 m3 93 Total 284 Per 94.6667




