C++ Program to enter Student Details using Virtual Class

By | 28.12.2016

Student Details using Virtual Class


Write a C++ Program to enter student details using Virtual Class. C++ Program to enter student marks and roll no. using Virtual Class. Here’s a Simple Program to enter student details using Virtual Class in C++ Programming Language.


What is Class and Objects in C++?


  • Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class.
  • The variables inside class definition are called as data members and the functions are called member functions.
  • Class is just a blue print, which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.

  • Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
  • Each object has different data variables. Objects are initialized using special class functions called Constructors.

Below is the source code for C++ Program to enter student details using Virtual Class which is successfully compiled and run on Windows System to produce desired output as shown below :


SOURCE CODE : :


/*  C++ Program to enter student details using Virtual Class  */

#include<iostream>

using namespace std;

class student

{
    protected:

        int roll_no;

    public:

        void get_no(int x)
        {
            roll_no=x;
        }

        void put_no()
        {
            cout<<"\nRoll Number :: "<<roll_no<<"\n";

        }
};

class test: virtual public student
{
    protected:
        float sub_marks;

    public:
        void get_submarks(float y)
        {
            sub_marks=y;
        }

        void put_submarks()
        {
            cout<<"\nSubject Marks :: "<<sub_marks<<"\n";
        }
};




class sports: public virtual student
{
    protected:
        float sp_marks;

    public:
        void get_spmarks(float z)
        {
            sp_marks=z;
        }

        void put_spmarks()
        {
            cout<<"\nSports Marks :: "<<sp_marks<<"\n";
        }

};

class result: public test, public sports
{

    float total_marks;

    public:
        void put_result()
        {
            total_marks=sub_marks+sp_marks;
            put_no();

            put_submarks();
            put_spmarks();
            cout<<"\nTotal Marks :: "<<total_marks<<"\n";
        }
};


int main()
{
    result R;
    R.get_no(34);
    R.get_submarks(67.2);
    R.get_spmarks(98.9);
    R.put_result();

    return 0;
}

OUTPUT : :


/*  C++ Program to enter student details using Virtual Class  */

Roll Number :: 34

Subject Marks :: 67.2

Sports Marks :: 98.9

Total Marks :: 166.1

Process returned 0

Above is the source code and output for C++ Program to enter student details using Virtual Class which is successfully compiled and run on Windows System to produce desired output.

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach upto you in the short interval.


Thanks for reading the post….

0 0 vote
Article Rating
Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Jayanth

how to display best of two tests marks by this virtual base class .. instead of sports marks take 2nd test .. and each test has objective and descriptive. … then how to get best of them 2…. ??
output should print any of best Discriptive and objective marks..