class - krishnaramb/cplusplus GitHub Wiki

class

  • A class is a user-defined datatype which groups together related pieces of information
  • An instance is an occurrence of a class. Different instances can have their own set of values in their fields.

In C++, struct and class are essentially the same, except structs’ default access modifier is public

class Point {
    public:
    double x;
    double y;
};
struct Point {
    double x;
    double y;
};

essentially these two are same thing

  • struct: public by default
  • class: private by default
struct Point {
    double x, y;
};

// equivalent to
struct Point {
    public:
    double x, y;
    };
class Point {
    double x, y;
};
//equivalent to
class Point {
    private:
    double x, y;
};

Q. Find the issue of the following code?

#include<iostream>
#include<string.h>
using namespace std;

class Vehicle{
protected:
  string license;
  int year;
public:
  Vehicle(const string & mylicense, const int myyear):license{mylicense},year{myyear}{cout<<"supperclass const called";}
  const string getDesc() {return license + "from";}
  const string & getLicense() const{return license;}
  const int getYear() const{return year;}

};

class Car:Vehicle{
  string style;

public:
  Car(const string & mylicense,const int my_year, const string & mystyle):Vehicle(mylicense,my_year),style{mystyle}{}
  const string & getStyle() const{return style;}

};
  int main()
  {
    Car car1("california",2013,"Honda");
    cout<<car1.getStyle();
    cout<<car1.getDesc();
    cout<<car1.getYear();
    return 0;
  }

Polymorphism

Polymorphism means “many shapes.” It refers to the ability of one object to have many types.:revolving_hearts: :punch: If we have a function that expects a Vehicle object, we can safely pass it a Car object, because every Car is also a Vehicle. Likewise for references and pointers: anywhere you can use a Vehicle *, you can use a Car *.

Q. what is output of the following code for function overriding?

#include<iostream>
#include<string.h>
using namespace std;

class Vehicle{
protected:
  string license;
  int year;
public:
  Vehicle(const string & mylicense, const int myyear):license{mylicense},year{myyear}{cout<<"supperclass constrcutor called";}
  const string getDesc() {return license + " Vehicle version of the func call ";}
  const string & getLicense() const{return license;}
  const int getYear() const{return year;}

};

class Car:public Vehicle{
  string style;

public:
  Car(const string & mylicense,const int my_year, const string & mystyle):Vehicle(mylicense,my_year),style{mystyle}{}
  const string & getStyle() const{return style;}
  const string getDesc() const {return style+" "+ license+ " car version of the func call";} //override the superclass function

};
  int main()
  {
    Car car1("california",2013,"Honda");
    cout<<car1.getStyle()<<"\n";
    cout<<car1.getDesc()<<"\n";
    cout<<car1.getYear()<<"\n";



    Car c("ohio",2003,"civic");
    Vehicle *vptr = &c;
    cout<<"\n\n"<<vptr->getDesc();//which version of getDesc will be called here?


    Car *cptr = &c;
    cout<<"\n\n"<<cptr->getDesc();//which version of getDesc will be called here?

    return 0;
  }

Because vptr is declared as a Vehicle *, this will call the Vehicle version of getDesc, even though the object pointed to is actually a Car.:punch: Usually we’d want the program to select the correct function at runtime based on which kind of object is pointed to. We can get this behavior by adding the keyword virtual before the method definition:

Base class constructors are automatically called for you if they have no argument. If you want to call a superclass constructor with an argument, you must use the subclass's constructor initialization list. Unlike Java, C++ supports multiple inheritance, so the base class must be referred to by name, rather than "super()".

class SuperClass
{
    public:
        SuperClass(int foo)
        {
            // do something with foo
        }
};

class SubClass : public SuperClass
{
    public:
        SubClass(int foo, int bar)
        : SuperClass(foo)    // Call the superclass constructor in the subclass' initialization list.
        {
            // do something with bar
        }
};
⚠️ **GitHub.com Fallback** ⚠️