is it polymorphism in oop

Thread Starter

John99407

Joined Jul 12, 2019
77
I have extended previous example in other thread to understand polymorphism in oop

C:
#include <iostream>
#include <string>
using namespace std;

class Vehicle {
  public:
    void WhoUseVehicle() {
      cout << "Person use vehicles\n" ;
    }
};

class PoliceCar : public Vehicle {
  public:
    void WhoUseVehicle() {
      cout << "Police officer use Police car \n" ;
    }
};

class SportCar : public Vehicle {
  public:
    void WhoUseVehicle () {
      cout << "Sport player Use sport Car\n" ;
    }
};

int main() {
  Vehicle mycar;
  PoliceCar ranger;
  SportCar jaguar;
 
  mycar.WhoUseVehicle();
  ranger.WhoUseVehicle();
  jaguar.WhoUseVehicle();
 
  return 0;
}
is it polymorphism in oop ? What will be the simple definition of polymorphism ?
 

Thread Starter

John99407

Joined Jul 12, 2019
77
Hi KeithWalker

How many years of experience do you have in c++ development ?
Do you write c++ program for microcontroller / PC ?
 

KeithWalker

Joined Jul 10, 2017
3,097
Hi KeithWalker

How many years of experience do you have in c++ development ?
Do you write c++ program for microcontroller / PC ?
I worked for Hewlett Packard in test and measurement from 1971 until I retired in 2003. They introduced the HPIB (later to become GPIB) instrument control bus in the mid 70's. Around the same time, the first practical microprocessor chips became available. That is when I started to teach myself how to program. I learned how to write 4-bit and 8-bit machine code programs using a very crude single pass assembler. I learned cobol, pascal, basic, forth, fortran, dos, c, c++, matlab, visual basic, Agilent VEE, and Labview plus a few very specialized ones. The last three of those are OOPs. I am not sure exactly when I learned each one. It was as needed and spread over 32 years. I was a systems engineer and a technical consultant. I designed test systems for customers who manufactured a wide range of products (everything from paper-pulp to nuclear fuel rods). I designed the test systems, had them assembled and then I wrote the software to make the measurements and test the results.
I still play around with analog and digital circuits and microprocessors as a hobby. I use whatever language is the best fit for what I am designing. This is the last thing I worked on. It's an auto-change record player from the 50's for my wife's doll house. Different 50's pop-tunes can be selected and the volume adjusted by pressing on the two knobs:
RecordPlayer 008.jpg

Regards,
Keith.
 
Last edited:

xox

Joined Sep 8, 2017
838
I have extended previous example in other thread to understand polymorphism in oop

C:
#include <iostream>
#include <string>
using namespace std;

class Vehicle {
  public:
    void WhoUseVehicle() {
      cout << "Person use vehicles\n" ;
    }
};

class PoliceCar : public Vehicle {
  public:
    void WhoUseVehicle() {
      cout << "Police officer use Police car \n" ;
    }
};

class SportCar : public Vehicle {
  public:
    void WhoUseVehicle () {
      cout << "Sport player Use sport Car\n" ;
    }
};

int main() {
  Vehicle mycar;
  PoliceCar ranger;
  SportCar jaguar;

  mycar.WhoUseVehicle();
  ranger.WhoUseVehicle();
  jaguar.WhoUseVehicle();

  return 0;
}
is it polymorphism in oop ? What will be the simple definition of polymorphism ?
No. Truly polymorphic objects in C++ utilize "virtual functions". Just try attaching a Vehicle* to one of those in your example and then print through it to see what I mean. What you have there is just simple inheritance.
 

Thread Starter

John99407

Joined Jul 12, 2019
77
If you remove ": public Vehicle " the results will be the same. So, no it's not polymorphism.
No. Truly polymorphic objects in C++ utilize "virtual functions". Just try attaching a Vehicle* to one of those in your example and then print through it to see what I mean. What you have there is just simple inheritance.
Why that is not polymorphism? I have read and seen video lectures there are two type of polymorphism. compile time and run time.

first example is compile time polymorphism. that can be achieved by function overloading

Run time polymorphismthat can be

This is an example of Run time that can be achieved by virtual function
Code:
class Vehicle
{
    public:
   virtual void Drive()
   {
      cout<<"Person is driving vehicle ";
      }
};

class PoliceCar : public Vehicle{
   public:
     void Drive(){ 
         cout << "Police officer driving Car";
     }
};

int main()
{
  Vehicle *M, C;
  PoliceCar C1;
  M = &C;

  M->Drive();
  M=&C1;

  return 0;
}
 

xox

Joined Sep 8, 2017
838
Why that is not polymorphism? I have read and seen video lectures there are two type of polymorphism. compile time and run time.

first example is compile time polymorphism. that can be achieved by function overloading

Run time polymorphismthat can be

This is an example of Run time that can be achieved by virtual function
Code:
class Vehicle
{
    public:
   virtual void Drive()
   {
      cout<<"Person is driving vehicle ";
      }
};

class PoliceCar : public Vehicle{
   public:
     void Drive(){
         cout << "Police officer driving Car";
     }
};

int main()
{
  Vehicle *M, C;
  PoliceCar C1;
  M = &C;

  M->Drive();
  M=&C1;

  return 0;
}
Take a look at this example.

Code:
#include <iostream>
#include <string>
using namespace std;

void say(const string& line)
{
 cout << line << endl;
}

struct base
{
 virtual void polymorphic()
 {
  say("base::polymorphic");  
 }

 void notsomorphic()
 {
  say("base::notsomorphic");  
 }
};

struct derived : base
{
 virtual void polymorphic()
 {
  say("derived::polymorphic");  
 }

 void notsomorphic()
 {
  say("derived::notsomorphic");  
 }
};

int main()
{
 derived d;
 say("Calling functions from local symbol 'd':");
 d.polymorphic();
 d.notsomorphic();
 base& b = d;
 say("Now calling through base reference 'b':");
 b.polymorphic();
 b.notsomorphic();
}
And here's the output:

Code:
Calling functions from local symbol 'd':
derived::polymorphic
derived::notsomorphic
Now calling through base reference 'b':
derived::polymorphic
base::notsomorphic
In other words without the `virtual` keyword the polymorphic behavior is limited to local symbols, which isn't usually very useful.
 
Last edited:

xox

Joined Sep 8, 2017
838
I explained pretty clearly why and that article basically says the same thing. You need virtual functions to get true runtime polymorphism in C++...
 
Top