C++: Classes And Objects

Nested classes ( Class within a class )

Question:1
C++ program to declare a class named student with attributes as roll and name. Declare another class named physical within the class student with attributes as age, height and weight. Take input for the details and display them.
Student(roll,name)
      Physical(age,ht,wt)
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 student::physical::read()
{
    cout<<"Enter age, height and weight ";
    cin>>age>>ht>>wt;
}
void student::physical::show()
{
    cout<<"age "<<age<<" height "<<ht<<" weight "<<wt<<endl;
}
int main()
{
    student s;
    student::physical p;
    s.read(); p.read();
    s.show(); p.show();
    return(0);
}
/* Output */

Enter roll and name 1002
Sunil
Enter age, height and weight 2
5
3
roll 1002 name Sunil
age 2 height 5 weight 3
 

Question:2
Declare a class named employee with attributes as empno and name. Declare another class named office within the class employee 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 employee::office::read()
{
	cout<<"Enter dno, dname and salary ";
	cin>>dno>>dname>>sal;
}
void employee::office::show()
{
	cout<<"Dno "<<dno<<" Dname "<<dname<<" Salary "<<sal<<endl;
}
int main()
{
	employee e;
	employee::office o;
	e.read(); o.read();
	e.show(); o.show();
	return(0);
}
/* Output */

Enter empno and name 1002
Kapil
Enter dno, dname and salary 50
Purchase
65000
Empno 1002 Name Kapil
Dno 50 Dname Purchase Salary 65000

Question:3
Declare a class named student with attributes as roll and name. Declare another class within the class student named marks with attributes as m1,m2,m3,total and per. Take input for the details and display them.
Student(roll,name)
Marks(m1,m2,m3,total,per)
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 student::marks::read()
{
    cout<<"Enter m1,m2 and m3 ";
    cin>>m1>>m2>>m3;
}
void student::marks::cal()
{
    total=m1+m2+m3;
    per=total/3;
}
void student::marks::show()
{
    cal();
    cout<<"m1 "<<m1<<" m2 "<<m2<<" m3 "<<m3<<endl;
    cout<<"Total "<<total<<" Per "<<per<<endl;
}
int main()
{
    student s;
    student::marks m;
    s.read(); m.read();
    s.show(); m.show();
    return(0);
}
/* Output */

Enter roll and name 1001
Amit
Enter m1,m2 and m3 98
97
95
roll 1001 name Amit
m1 98 m2 97 m3 95
Total 290 Per 96.6667