9/13/2016

Hacker Rank solution for Virtual Functions In C++

Problem:- Virtual Functions Solution Hacker Rank Solution or Virtual Functions Solution hacker rank solution c++ orMulti Level Inheritance in java or Virtual Functions Solution solution or Virtual Functions Solution solution in C++ or Virtual Functions Solution hacker rank solution in Java or Virtual Functions Solution hacker rank solution c++ or Virtual Functions Solution hacker rank solution c++ or Virtual Functions Solution program in c or Virtual Functions Solution solution in C or Hacker Rank Solution For Virtual Functions Solution in C++ or Virtual Functions Solution in C++ or Hacker Rank solution for Virtual Functions Solution in C++.



Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen c++ editor in the top right drop down option.

Submit your solution here:- Click here


Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;
class Person
    {
    public:
    virtual void getdata() = 0;
    virtual void putdata() = 0;
    protected:
    string name;
    int age, sum=0;
   };
class Professor : public Person
    {
    public:
    Professor():
        id {current_id++}
    { }
    void getdata()
        {
        cin>>name>>age>>pub;
        }
    void putdata()
        {
        cout<<name<<" "<<age<<" "<<pub<<" "<<id<<endl;
        }
    private:
    int pub;
    int id;
    static int current_id;
   };
class Student : public Person
    {
    public:
    Student():
        marks(6),
        id {current_id++}
    { }
    void getdata()
        {
        int i;
        cin>>name>>age;
        for (auto& mark : marks)
        {
            cin >> mark;
            sum+=mark;
        }
        }
    void putdata()
        {
        int i;
        cout<<name<<" "<<age<<" "<<sum<<" "<<id<<endl;
        }
    private:
    vector<int> marks;
    int id;
    static int current_id;
};
int Professor::current_id {1};

int Student::current_id {1};

int main()
{

    int n, val;
    cin>>n; //The number of objects that is going to be created.
    Person *per[n];

    for(int i = 0;i < n;i++){

        cin>>val;
        if(val == 1){
            // If val is 1 current object is of type Professor
            per[i] = new Professor;

        }
        else per[i] = new Student; // Else the current object is of type //Student
        per[i]->getdata(); // Get the data from the user.
    }
    for(int i=0;i<n;i++)
        per[i]->putdata(); // Print the required output for each object.
    return 0;

}

Output:-

Hacker Rank solution for Virtual Functions In C++



You May Also Like

1. C++ Program To Find Cube Of Any Number Using Functions

2. C++ Program To Perform All Arithmetic Operations Using Functions

3. C++ Program To Find GCD (Greatest Common Divisor ) Using Functions

4. C++ Program To Find Fibonacci Series Using Functions

5. C++ Program To Swap Two Numbers Using Functions

6. C++ Program To Swap Two Numbers Without Using Third Variable Using Functions

7. C++ Program to Calculate Standard Deviation Using Function

No comments:

Post a Comment