You must Sign In to post a response.
  • Category: Programming

    Design classes in object oriented programming


    Learning object oriented programming? Want to design a class? Find response from experts on this page.

    Design a class 'university'. It has name and place as data members. Their member functions are for printing and getting data members. Derive 'department' class from 'university' class protected. Its data members are department name, no. of students, and number of staff. Member functions are for getting and printing data members. Derive one more class 'staff' from 'department', data members are name of staff, salary, experience (in years) and subject. Another class student is derived from 'department' as 'student'. Data members are name of student, years of course and total marks. These two classes are derived from parent class in private mode. Experts: Can you provide a C++ program for implementing the above scenario.
    Awaiting response.
  • #139911
    You can use the this coder for class in c++

    #include

    using namespace std;

    class programming
    {
    private:
    int variable;

    public:

    void input_value()
    {
    cout << "In function input_value, Enter an integer\n";
    cin >> variable;
    }

    void output_value()
    {
    cout << "Variable entered is ";
    cout << variable << "\n";
    }
    };

    main()
    {
    programming object;

    object.input_value();
    object.output_value();


    return 0;
    }

    Saket singh
    Everyday Learner

  • #140295
    Below is the program which uses concept of inheritance as per your requirement. In object oriented programming concept of re-usability is implemented by using inheritance.
    Please include iostream.h and conio.h header files respectively in the beginning of the program.

    #include
    #include
    #include
    class university
    {
    protected:
    char uname[20],place[20];
    public:
    void input(void)
    {
    cout<<"Enter university name and place where university is situated\n";
    cin>>uname>>place;
    }
    void output(void)
    {
    cout<<"\nName os univesity is "< cout<<"\nPlace of university is "< }
    };
    class department:protected university
    {
    protected:
    char deptname[20];
    int no_staff,no_students;
    public:
    void get(void)
    {
    input();
    cout<<"\nEnter name of department\n";
    cin>>deptname;
    cout<<"\nEnter count of department staff members\n";
    cin>>no_staff;
    cout<<"\nEnter count of department students\n";
    cin>>no_students;
    }
    void put(void)
    {
    output();
    cout<<"\nName of department is "< cout<<"\nCount of department staff members is "< cout<<"\nCount of department students is "< }
    };
    class staff:department
    {
    protected:
    char staffname[20],subject[20];
    float salary;
    int experience;
    public:
    void getdata(void)
    {

    get();
    cout<<"\nEnter name of the staff\n";
    cin>>staffname;
    cout<<"\nEnter salary of the staff\n";
    cin>>salary;
    cout<<"\nEnter experience of the staff\n";
    cin>>experience;
    cout<<"\nEnter the subject name thought by the staff\n";
    cin>>subject;
    }
    void putdata(void)
    {

    put();
    cout<<"\nName of the staff is "< cout<<"\nSalary of the staff is "< cout<<"\nExperience of the staff is "< cout<<"\nSubject name thought by the staff is "< }
    };
    class student:department
    {
    protected:
    int tmark;
    char year[20];
    public:
    void in(void)
    {
    get();
    cout<<"\nEnter the year of student(like first,second)\n";
    cin>>year;
    cout<<"\nEnter total marks obtained by the student\n";
    fflush(stdin);
    cin>>tmark;
    }
    void out(void)
    {
    put();
    cout<<"\nThe year of student is "< cout<<"\nTotal marks obtained by the student are "< }
    };

    void main(void)
    {
    clrscr();
    cout<<"\n1. Staff Information\n";
    cout<<"\n2. Student Information\n";
    int ch;
    cout<<"\nEnter your choice\n";
    cin>>ch;
    switch(ch)
    {
    case 1:
    class staff stf;
    stf.getdata();
    cout<<"Details of Staff is as follows\n";
    stf.putdata();
    break;
    case 2:
    class student stu;
    stu.in();
    cout<<"\nDetails of student is as follows\n";
    stu.out();
    break;
    default:
    cout<<"invalid choice\n";
    }
    getch();
    }

    As per your requirement derived class "department" is inherited in protected mode. So that all protected members of base class "university" becomes protected in derived class too. All public members of base class becomes protected in derived class because of protected inheritance. Staff and student classes are derived form department class in "private mode" so that all public members of department becomes private in staff and student class. As all public members of department class are functions and these functions when become private it is not possible to call them directly using object and dot operator. So these functions are called inside the public functions of staff/student class by using concept function within a function.

    Thanks for providing such a conceptional program for solving which gives me lot of pleasure.


  • Sign In to post your comments