1. Design
a class student with data members
roll, name and age. Use three arguments constructor to initialize student
object, and also use member function to read and display student detail.
Also design a
class foreignstudent that inherits student with its own data member
country. Use member function to display detail of foreign student.
Write a main
program to test above implementation.
#include<iostream>#include<cstring>#include<conio.h>using namespace std;
class Student{ protected: int roll; char name[40]; int age; public: Student(){} Student(int r,char *n, int a) { roll=r; strcpy(name,n); age=a; } void read_data() { cout<<"Roll = ";cin>>roll; cout<<"Name = ";cin>>name; cout<<"Age = ";cin>>age; } void put_data() { cout<<"Roll = "<<roll<<endl; cout<<"Name = "<<name<<endl; cout<<"Age = "<<age<<endl; }};class Foreignstudent:public Student{ private: char country[40]; public: Foreignstudent():Student(){} Foreignstudent(int r,char *n,int a, char *c):Student(r,n,a) { strcpy(country,c); } void read_data() { Student::read_data(); cout<<"Country = "; cin>>ws; cin.getline(country,40); } void put_data() { Student::put_data(); cout<<"Country = "<<country<<endl; }};int main(){ Foreignstudent fs1(101,"Prashant",22,"Nepal"); Foreignstudent fs2; cout<<"Enter detail of Foreignstudent fs2:\n"; fs2.read_data(); cout<<endl<<endl<<"================================="<<endl; cout<<"Details of students are"<<endl; cout<<"================================="<<endl; fs1.put_data(); cout<<"================================="<<endl; fs2.put_data(); cout<<"================================="<<endl; getch(); return 0;}
2. Design
a program as specified below:
i.
Design a class student with data members roll and name, and two member function to
read and display data members.
ii.
Derive a class Exam from student with
its own data members to stores marks in three subjects and member function to
read and display marks.
iii.
Design a third class result that inherits class
Exam, with its own data member total and
member function display() to display roll, name, marks and total..
#include<iostream>
#include<conio.h>
#include<cstring>
using namespace std;
class Student
{
protected:
int roll;
string name;
public:
void getdata()
{
cout<<"Roll=";cin>>roll;
cout<<"Name=";cin>>name;
}
void putdata()
{
cout<<"Roll= "<<roll<<endl;
cout<<"Name= "<<name<<endl;
}
};
class Exam: public Student
{
protected:
int oop;
int os;
int nm;
public:
void getmark()
{
cout<<"Enter marks in OOP:";cin>>oop;
cout<<"Enter marks in OS:";cin>>os;
cout<<"Enter marks in NM:";cin>>nm;
}
void putmark()
{
cout<<"Marks in OOP="<<oop<<endl;
cout<<"Marks in OS="<<os<<endl;
cout<<"Marks in NM="<<nm<<endl;
}
};
class Result: public Exam
{
private:
int total;
public:
void display()
{
total=oop+os+nm;
putdata();
putmark();
cout<<"Total Marks="<<total<<endl;
}
};
int main()
{
Result stu1;
cout<<"Enter details of stu1:"<<endl<<endl;
stu1.getdata();
stu1.getmark();
cout<<"\n\nDetails of stu1"<<endl;
cout<<"_______________"<<endl<<endl;
stu1.display();
getch();
return 0;
}
3. Write
a program as specified as below:
i.
Design a class staff with data member staff_id and level, and member function to
read display detail of staff.
ii.
Also design another class teacher with data
members teacher_id, subject and member function to read and display detail of
teacher.
iii.
Again design another class coordinator that
inherits teacher and staff with own data member program. Use member function to
read and display detail of coordinator.
Design a main
program to test above design.
#include<iostream>#include<conio.h>using namespace std;
class staff{ protected: int staff_id; int level; public: void get_staff_data() { cout<<"Staff ID=";cin>>staff_id; cout<<"Level=";cin>>level; } void put_staff_data() { cout<<"staff ID="<<staff_id<<endl; cout<<"Level="<<level<<endl; }};class teacher{ protected: int teacher_id; char subject[40]; public: void get_teacher_data() { cout<<"Teacher ID=";cin>>teacher_id; cout<<"Subject=";cin>>subject; } void put_teacher_data() { cout<<"Teacher ID="<<teacher_id<<endl; cout<<"Subject="<<subject<<endl; }};class coordinator:public staff,public teacher{ private: char program[40]; public: void get_coordinator_data() { get_staff_data(); get_teacher_data(); cout<<"Program=";cin>>program; } void put_coordinator_data() { put_staff_data(); put_teacher_data(); cout<<"Program="<<program<<endl; }};
int main(){ coordinator c; cout<<"Enter details of coordinator"<<endl; c.get_coordinator_data(); cout<<"\nDetails of coordinator\n_____________________\n"<<endl; c.put_coordinator_data(); getch(); return 0;}
4. Write
a program as specified below:
i.
Design a class student with data members roll and name, and two member function to
read and display data members.
ii.
Derive a class Exam from student with
its own data members to stores marks in three subjects and member function to
read and display marks.
iii.
Derive another class sport from student with
its own data member score in sport and function to read and display score.
iv.
Design another class result from exam and sport, with its own data member total and
average, and member function compute() to compute total and average and
display() to display detail.
//multipath inheritance
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
private:
int roll_no;
char name[20];
public:
void getdata()
{
cout<<"Roll N0.= ";cin>>roll_no;
cout<<"Name= ";cin>>name;
}
void putdata()
{
cout<<"Roll N0.= "<<roll_no<<endl;
cout<<"Name= "<<name<<endl;
}
};
class exam:virtual public student
{
protected:
double sub1,sub2,sub3;
public:
void getmark()
{
cout<<"Mark in sub1= ";cin>>sub1;
cout<<"Mark in sub2= ";cin>>sub2;
cout<<"Mark in sub3= ";cin>>sub3;
}
void putmark()
{
cout<<"Mark in sub1= "<<sub1<<endl;
cout<<"Mark in sub2= "<<sub2<<endl;
cout<<"Mark in sub3= "<<sub3<<endl;
}
};
class sport:virtual public student
{
protected:
double sport_score;
public:
void getscore()
{
cout<<"Score in Sport= ";cin>>sport_score;
}
void putscore()
{
cout<<"Score in Sport= "<<sport_score<<endl;
}
};
class result:public exam,public sport
{
private:
double total,avg;
public:
void compute()
{
total=sub1+sub2+sub3+sport_score;
avg=total/4;
}
void display()
{
putdata();
putmark();
putscore();
cout<<"Total= "<<total<<endl;
cout<<"Average= "<<avg<<endl;
}
};
int main()
{
result r;
cout<<"Enter details of a student"<<endl<<endl;
r.getdata();
r.getmark();
r.getscore();
r.compute();
cout<<"\nDetail of result r is :\n";
cout<<"________________________\n";
r.display();
getch();
return 0;
}
5. Design
a class employee with data members id and name, and member functions getdata()
and putdata() to read and display data members. Design another class company
with data members company_id, company_name and employee and member function
getdata() and putdata() to read and display detail of company. Write a main
program in C++ to test above design.
#include<iostream>
#include<conio.h>
using namespace std;
class Employee
{
private:
int employee_id;
char employee_name[40];
public:
void getdata()
{
cout<<"Employee ID= ";cin>>employee_id;
cout<<"Employee Name= ";cin>>employee_name;
}
void putdata()
{
cout<<"ID= "<<employee_id<<endl;
cout<<"Name= "<<employee_name<<endl;
}
};
class Company
{
private:
int company_id;
char company_name[40];
Employee E;
public:
void getdata()
{
cout<<"Company ID= ";cin>>company_id;
cout<<"Company Name= ";cin>>company_name;
E.getdata();
}
void putdata()
{
cout<<"Company ID= "<<company_id<<endl;
cout<<"Company Name= "<<company_name<<endl;
E.putdata();
} };
int main()
{
Company C;
cout<<"Enter Details of company C"<<endl<<endl;
C.getdata();
cout<<"\nDetails of company C\n____________________\n"<<endl;
C.putdata();
getch();
return 0;
}
6. Imagine
a publishing company that markets both book and audio cassette version of it
works. Create a class publication that stores the title and a
price of a publication. From this class derive twp classes : book, which adds page count, and a tape, which adds a time in minutes.
Each of these three classes should have a getdata() function to get its data
from user at the keyboard, and putdata()
function to display its data.
In the base
class these member functions have to be defined as virtual functions. Write a
main program models the class hierarchy for the company and processes objects
of these classes using pointers the base class.
#include<iostream>
#include<conio.h>
using namespace std;
class publication
{
protected:
char title[40];
float price;
public:
virtual void getdata()
{
cout<<"Title=";cin>>title;
cout<<"Price=";cin>>price;
}
virtual void putdata()
{
cout<<"Title="<<title<<endl;
cout<<"Price="<<price<<endl;
}
};
class book:public publication
{
private:
int page_count;
public:
void getdata()
{
cout<<"Page Count=";cin>>page_count;
}
void putdata()
{
cout<<"Page Count="<<page_count<<endl;
}
};
class tape:public publication
{
private:
int time;
public:
void getdata()
{
cout<<"Time in minutes=";cin>>time;
}
void putdata()
{
cout<<"Time in minutes="<<time<<endl;
}
};
int main()
{
book objB;
tape objT;
publication *ptr,objP;
cout<<"Enter Details of publication company"<<endl<<endl;
ptr=&objP;
ptr->getdata();
cout<<"\nEnter Details of the book"<<endl<<endl;
ptr=&objB;
ptr->getdata();
cout<<"\nEnter Details of the tape"<<endl<<endl;
ptr=&objT;
ptr->getdata();
cout<<"\nDetails of publication company\n==============================\n"<<endl;
ptr=&objP;
ptr->putdata();
cout<<"\nDetails of the book\n===================\n"<<endl;
ptr=&objB;
ptr->putdata();
cout<<"\nDetails of the tape\n===================\n"<<endl;
ptr=&objT;
ptr->putdata();
getch();
return 0;
}
No comments:
Post a Comment