Object Oriented Programming Practical 11

Question 1
Write a C++ program to define a class "Student" having data members roll_no, name. Derive a class "Marks" from "Student" having data members m1,m2,m3, total and percentage. Accept and display data for one student.
student.cpp
C
				#include<iostream.h>
#include<conio.h>

class student
{
	int rollno;
	char name[100];

	public:
	void accept()
   {
		cout<<"\nEnter name of student: ";
		cin>>name;
		cout<<"\nEnter roll number of student: ";
		cin>>rollno;
   }

   void display()
	{
		cout<<"\nStudent name: "<<name;
		cout<<"\nStudent rollno: "<<rollno;
	}
};

class marks:public student
{
 	int m1,m2,m3,total;
	float percent;

   public:
   void getdata()
   {
		cout<<"\nEnter marks 1: ";
		cin>>m1;
		cout<<"\nEnter marks 2: ";
		cin>>m2;
		cout<<"\nEnter marks 3: ";
		cin>>m3;
   }

   void putdata()
	{
		total=m1+m2+m3;
		percent=(total*100)/300.00;
		cout<<"\nTest percent : "<<percent<<endl;
	}
};

int main()
{
	clrscr();
	marks m;
	m.accept();
	m.getdata();
	m.display();
	m.putdata();
	getch();
	return 0;
}

			
Output
Question 2
Write a C++ program to implement following Multilevel Inheritance
person.cpp
C
				#include<iostream.h>
#include<conio.h>

class person
{
   int age;
   char name[100],gender[10];

	public:
   void accept()
   {
		cout<<"\nEnter name of person: ";
      cin>>name;
      cout<<"\nEnter age of person: ";
      cin>>age;
		cout<<"\nEnter gender: ";
      cin>>gender;
   }

   void display()
	{
		cout<<"\nPerson name: "<<name;
		cout<<"\nPerson age: "<<age;
		cout<<"\nPerson gender: "<<gender;
	}
};

class employee:public person
{
 	int emp_id;
   float salary;
   char company[50];

   public:
   void getdata()
   {
		cout<<"\nEnter employee id: ";
      cin>>emp_id;
      cout<<"\nEnter company name: ";
      cin>>company;
		cout<<"\nEnter employee salary: ";
      cin>>salary;
   }

   void putdata()
	{
		cout<<"\nEmployee id: "<<emp_id;
		cout<<"\nEmployee company : "<<company;
		cout<<"\nEmployee salary : "<<salary;
	}
};

class programmer:public employee
{
   int no_of_prog_lang_known;

	public:
   void get()
   {
		cout<<"\nEnter number of programmign languages known: ";
      cin>>no_of_prog_lang_known;
   }

   void put()
	{
		cout<<"\nNumber of programmign languages known: "<<no_of_prog_lang_known;
	}
};

void main()
{
	clrscr();
   programmer p;
   p.accept();
   p.getdata();
   p.get();
   p.display();
   p.putdata();
   p.put();
   getch();
}

			
Output
Note: Change array size and for loop for 10 accounts.

Leave a Comment

Your email address will not be published. Required fields are marked *