HSC – VIRTUAL FUNCTION IN C++ | EXP-4

Hey there, it’s your friendly neighborhood computer engineer, Vinit Pandey! I’m the mastermind behind smmwizardpro.com and edu4maharashtra.in – websites so cool they’ll make your head spin. And if you’re not following me on Instagram at vinit.bro, well, let’s say I am missing you their.

So if you want to level up your tech game, just hit me up. I promise I won’t bite – unless you’re a computer virus, in which case I’ll destroy you faster than you can say “blue screen of death.”
Author Name

Write a program in C++ using virtual function. The program must declare p to be a pointer to objects of the base class person First, the program must assign p to point an instance x (name of the person e.g. “BOB”) of class person. The program must then assign p to point at an instance y (name of student, e.g. “TOM”) of the derived class student. Define a print () function in the base class such that it invokes the same base class function to print the name of the person by default. The second call for the same should evoke the derived class the name of the student

#include <iostream.h>  // include the iostream.h header file for console I/O
#include <conio.h>     // include the conio.h header file for console I/O functions
#include <string.h>    // include the string.h header file for string manipulation functions
#include <stdio.h>     // include the stdio.h header file for standard I/O functions

class Person {          // declare a class called Person
protected:              // protected access specifier for member variables
    char name[20];      // declare a character array for storing the person's name
public:                 // public access specifier for member functions
    Person(char n[20]) {   // declare a constructor that takes a character array as an argument
        strcpy(name, n);   // copy the contents of the character array into the name member variable
    }
    virtual void print() { // declare a virtual function for printing the person's name
        cout << "Person's name is: " << name << endl; // print the person's name to the console
    }
};

class Student : public Person { // declare a class called Student that inherits from Person
private:                // private access specifier for member variables
    char studentName[20]; // declare a character array for storing the student's name
public:                 // public access specifier for member functions
    Student(char n[20], char sn[20]) : Person(n) { // declare a constructor that takes two character arrays as arguments
        strcpy(studentName, sn); // copy the contents of the second character array into the studentName member variable
    }
    void print() {  // declare a function for printing the person's and student's names
        cout << "Person's name is: " << name << endl;             // print the person's name to the console
        cout << "Student's name is: " << studentName << endl;     // print the student's name to the console
    }
};

int main() {            // the main function that executes the program
    clrscr();           // clear the console screen
    Person *p;          // declare a pointer to a Person object
    Person x("BOB");    // declare an instance of Person with name "BOB"
    Student y("TOM", "TOM'S STUDENT NAME"); // declare an instance of Student with name "TOM" and student name "TOM'S STUDENT NAME"
    p = &x;             // assign the pointer to the address of x (a Person object)
    p->print();         // call the print function on the pointer, which will call the base class function and print "Person's name is: BOB"
    p = &y;             // assign the pointer to the address of y (a Student object)
    p->print();         // call the print function on the pointer, which will call the derived class function and print "Person's name is: TOM" and "Student's name is: TOM'S STUDENT NAME"
    getch();            // wait for the user to press a key
    return 0;           // exit the program with a status of 0 (successful)
}

Leave a Comment

Scroll to Top