Answer to Question #270507 in C++ for Alishba

Question #270507

Write a class student that contains the attribute teacher name, age and address. It also contains member function to input and display its attribute. Write another class teacher Write another class Writer that contains the attributes writer name, address and number of books written by him. It also contains member function to input and display its attributes. Write a third class Scholar that inherits both student and teacher classes.

1
Expert's answer
2021-11-23T07:57:50-0500
#include<iostream>
#include<string>
using namespace std;

class Student
{
	string teachName;
	int age;
	string address;
public:
	Student():teachName(""),age(0),address(""){}
	void SetAtr()
	{
		cout<<"Please, enter a teacher name: ";
		cin>>teachName;
		cout<<"Please, enter an age: ";
		cin>>age;
		cout<<"Please, enter an address: ";
		cin.ignore(256,'\n');
		getline(cin,address);
	}
	void Display()
	{
		cout<<"\nTeacher name is\t\t"<<teachName
			<<"\nAge is\t\t\t"<<age
			<<"\nAddress is\t\t"<<address;
	}
};

class Writer
{
	string writerName;
	int bookNum;
	string address;
public:
	Writer():writerName(""),bookNum(0),address(""){}
	void SetAtr()
	{
		cout<<"Please, enter a writer name: ";
		cin>>writerName;
		cout<<"Please, enter number of books: ";
		cin>>bookNum;
		cout<<"Please, enter an address: ";
		cin.ignore(256,'\n');
		getline(cin,address);
	}
	void Display()
	{
		cout<<"\nWriter name is\t\t"<<writerName
			<<"\nNumber of books is\t"<<bookNum
			<<"\nAddress is\t\t"<<address;
	}
};

class Scholar:public Student, public Writer{};

int main()
{
	Scholar s;
	s.Student::SetAtr();
	s.Writer::SetAtr();
	s.Student::Display();
	s.Writer::Display();
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS