0 votes
87 views
in Practical Questions by (98.9k points)
practical session(class hierarchy & virtual function)

1 Answer

0 votes
by (98.9k points)
 
Best answer

class hierarchy:

 

 

#include<iostream>
#include <string>
using namespace std;

class Student
{
protected:
  int rollno;
public:
  void setno (int);
  void putno (void);
};

void
Student::setno (int a)
{
  rollno = a;
}

void
Student::putno ()
{
  cout << "\nRoll No. : " << rollno;
}

class Test:public Student
{
protected:
  float sub1, sub2;
public:
  void setmarks (float, float);
  void putmarks (void);
};

void
Test::setmarks (float x, float y)
{
  sub1 = x;
  sub2 = y;
}

void
Test::putmarks ()
{
  cout << "\nMarks in Sub1 : " << sub1;
  cout << "\nMarks in Sub2 : " << sub2;
}

class Sports
{
protected:
  float score;
public:
  void setscore (float);
  void putscore (void);
};

void
Sports::setscore (float s)
{
  score = s;
}

void
Sports::putscore ()
{
  cout << "\nSports score marks : " << score;
}

class Result:public Test, public Sports
{
private:
  float total;
public:
  void display (void);
};

void
Result::display ()
{
  total = sub1 + sub2 + score;
  putno ();
  putmarks ();
  putscore ();
  cout << "\nTotal marks = " << total;
}

int
main ()
{
  string name;
  int rno;
  float s1, s2, sc;
  Result r;
  cout << "Enter Student Information :" << endl;
  cout << "name of student : ";
  getline (cin, name);
  cout << "\nRoll No. : ";
  cin >> rno;
  cout << "Marks in first subject : ";
  cin >> s1;
  cout << "Marks in second subject : ";
  cin >> s2;
  cout << " score in sports : ";
  cin >> sc;
  r.setno (rno);
  r.setmarks (s1, s2);
  r.setscore (sc);
  cout << "\nThe Student Details are as follows : " << endl;
  cout << "name of student : " << name;
  r.display ();

}

 

visual function

 

#include <iostream>

class Person {
public:
    virtual void greet() const {
        std::cout << "Hello, I am a person." << std::endl;
    }
};

class Tom : public Person {
public:
    void greet() const override {
        std::cout << "Hi, I am Tom!" << std::endl;
    }
};

class Bob : public Person {
public:
    void greet() const override {
        std::cout << "Hey there, I am Bob!" << std::endl;
    }
};

int main() {
    Person* person1 = new Tom();
    Person* person2 = new Bob();

    person1->greet();
    person2->greet();

    delete person1;
    delete person2;

    return 0;
}
 

Related questions

0 votes
1 answer 77 views

Doubtly is an online community for engineering students, offering:

  • Free viva questions PDFs
  • Previous year question papers (PYQs)
  • Academic doubt solutions
  • Expert-guided solutions

Get the pro version for free by logging in!

5.7k questions

5.1k answers

108 comments

535 users

...