Search
 
SCRIPT & CODE EXAMPLE
 

CPP

polymorphism c++ virtual

// C++ program to demonstrate how we will calculate
// area of shapes without virtual function
#include <iostream>
using namespace std;
 
// Base class
class Shape {
public:
    // parameterized constructor
    Shape(int l, int w)
    {
        length = l;
        width = w;
    }
    int get_Area()
    {
        cout << "This is call to parent class area
";
        // Returning 1 in user-defined function means true
        return 1;
    }
 
protected:
    int length, width;
};
 
// Derived class
class Square : public Shape {
public:
    Square(int l = 0, int w = 0)
        : Shape(l, w)
    {
    } // declaring and initializing derived class
    // constructor
    int get_Area()
    {
        cout << "Square area: " << length * width << '
';
        return (length * width);
    }
};
// Derived class
class Rectangle : public Shape {
public:
    Rectangle(int l = 0, int w = 0)
        : Shape(l, w)
    {
    } // declaring and initializing derived class
    // constructor
    int get_Area()
    {
        cout << "Rectangle area: " << length * width
             << '
';
        return (length * width);
    }
};
 
int main()
{
    Shape* s;
 
    // Making object of child class Square
    Square sq(5, 5);
 
    // Making object of child class Rectangle
    Rectangle rec(4, 5);
    s = &sq; // reference variable
    s->get_Area();
    s = &rec; // reference variable
    s->get_Area();
 
    return 0; // too tell the program executed
    // successfully
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: how to read qlistwidget in c++ 
Cpp :: how can I convert each and every element of string push into set in c++? 
Cpp :: c++ string not printing 
Cpp :: rand function c++ 
Cpp :: how to make a goto area in c++ 
Cpp :: Lapindromes codechef solution in c++ 
Cpp :: c++ to c converter 
Cpp :: (/~/+|/+$/g, ') 
Cpp :: c++ to c converter online free 
Cpp :: ex:c++ gcc start adress 
Cpp :: inverse lerp c++ 
Cpp :: how to implement stack 
Cpp :: c++ loop vector iterator 
Cpp :: c++ map key exists 
Cpp :: palindrome string 
Cpp :: split string by delimiter cpp 
Cpp :: c++ return statement 
Cpp :: cpp set time 
Cpp :: is the c++ 20 char te same as the old one 
C :: how to use gotoxy in c language 
C :: manifest orientation portrait 
C :: Invalid public key for CUDA apt repository 
C :: come creare variabili casuali in c 
C :: How to generate a random array in c 
C :: how to find sum of two nums 
C :: block a website on mac 
C :: how to turn off zsh 
C :: Graphics in C Draw Circle 
C :: function for calculate the average and standard deviation of table 
C :: c to llvm 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =