Tuesday, 13 September 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

Extreme Recommended:- Like our Facebook Page or Join our Facebook Group and Google plus Community for up-to-date. If you have any Query or Question you can ask in the group, I will Try To Solve your Query and try to answers of your Questions withing 24 Hours, You can also Email me or comment below Please suggest to your Friends to join and like our page and don't forget to Subscribe. Enter your Email and click to subscribe.

Ghanendra Yadav

Ghanendra Yadav

Hello, I Am Ghanendra Yadav Owner of This Blog, I am professional Blogger and Programmer. I Love Programming, Logo Making, And Banner Designing. My Highest Qualification is MCA From NIT Warangal. You Can Find Me On Social Media Through Below Link And If You Have Any Query Related To Programming And Other Subject Comment Below or You Can Mail Me I Will Try To Answer Within 24 Hours Email:- yghanendra@student.nitw.ac.in

Find me on Social Media

Facebook | Twitter | Google+ | RSS Feed

No comments:

Post a Comment