Derived Classes

Class Definitions

Rectangle

  • ### Class Definition

  class Rectangle {
  private:
      int numVertices;
      float *xCoord, *yCoord;
  public:
      void set(float *x, float *y, int nV);
      float area();
  };

Triangle

  • ### Class Definition

  class Triangle {
  private:
      int numVertices;
      float *xCoord, *yCoord;
  public:
      void set(float *x, float *y, int nV);
      float area();
  };

Polygon

  • ### Class Definition

  class Polygon {
  private:
      int numVertices;
      float *xCoord, *yCoord;
  public:
      void set(float *x, float *y, int nV);
  };

Inheritance Concepts

  • Inheritance allows new classes to be built from existing classes by deriving from them.

  • Benefits include:

    • Specialization: Extending existing functionality.

    • Augmentation: Adding new features to existing types.

Class Hierarchy

Base and Derived Classes

  • ### Syntax for Derived Classes

  class DerivedClassName : access-level BaseClassName {
      // class members
  };
  • Where access-level specifies type of inheritance (default is private).

    • Any class can serve as a base class; derived classes can also act as base classes.

Class Access and Inheritance

Access Control

  • Members of base classes can be inherited with different access permissions based on inheritance type:

    • Private: No access from derived class.

    • Protected: Access within derived class and its sub-classes.

    • Public: Access in any class.

Class Member Inheritance

  • Every member of a base class can be inherited by the derived class depending on access rights.

  • Constructors and destructors of base classes are not inherited.

Example Class Implementation

3D Point Class

  • ### Class Definition

  class Point {
  protected:
      int x, y;
  public:
      void set(int a, int b);
  };

  class 3D-Point : public Point {
  private:
      double z;
  };

Constructor Rules for Derived Classes

  • The default constructor and destructor of the base class are automatically called when a derived class object is created or destroyed.

  • You can specify the constructor of the base class using:

  DerivedClassCon (derivedClass args) : BaseClassCon (baseClass args) {
      //Derived class constructor body
  }
  • ## Example

  class A {
  public:
      A() { cout << "A:default" << endl; }
      A(int a) { cout << "A:parameter" << endl; }
  };

  class B : public A {
  public:
      B(int a) { cout << "B" << endl; }
  };

  B test(1);
  // Output:
  // A:default
  // B

Overriding Functions

Derived Class Members

  • A derived class can have its own members in addition to inherited members.

  • ### Method Overriding Example

  class A {
  public:
      void print() { cout << "From A" << endl; }
  };

  class B : public A {
  public:
      void print() { cout << "From B" << endl; }
  };
  • Inherited objects calling overridden methods will execute the derived class version.

Putting it All Together

Example: Time Class

  • ### Base Class Implementation

  class Time {
  public:
      void Set(int h, int m, int s);
      void Increment();
      void Write() const;
      Time(int initH, int initM, int initS);
      Time(); // default constructor
  protected:
      int hrs, mins, secs;
  };
  • ### Derived Class Implementation

  class ExtTime : public Time {
  public:
      void Set(int h, int m, int s, ZoneType timeZone);
      void Write() const; // overridden
      ExtTime(int initH, int initM, int initS, ZoneType initZone);
      ExtTime(); // default constructor
  private:
      ZoneType zone; // additional data member
  };

Example Usage in Main

  • Creating time objects:

  ExtTime thisTime(8, 35, 0, PST);
  thisTime.Write();

Polymorphism

Overview

  • Polymorphism allows objects to be treated as instances of their parent class, enabling methods to be called without knowing their exact derived types.

  • Achieved using virtual functions that facilitate dynamic binding or late binding.

Declaring Virtual Functions

  • To enable polymorphism, declare a base class method as virtual:

  virtual void Write() { /* implementation */ }

Example Implementation

  • Use of polymorphism:

  void Print(Time *someTime) {
      cout << "Time is ";
      someTime->Write();
      cout << endl;
  }

Notes on Virtual Functions

  • When a class uses virtual functions, it also requires that its destructor is declared virtual to ensure proper cleanup of derived classes.

Abstract Classes and Pure Virtual Functions

Concept of Abstract Classes

  • An abstract class cannot be instantiated directly. It serves as a blueprint for derived classes.

  • Pure virtual function indicates that the derived class must implement that function:

  virtual void draw() = 0;

Example of Abstract Class Usage

  • Abstract shape class implementation:

  class Shape {
  public:
      virtual void draw() = 0; // pure virtual function
  };
  • Classes derived from Shape must implement the draw method:

  class Circle : public Shape {
  public:
      void draw() { /* Circle specific implementation */ }
  };

Classes like Rectangle, Triangle, and Polygon are defined with private data members (e.g., numVertices, xCoord, yCoord) and public methods (e.g., set, area).

Inheritance allows new classes to be built from existing ones, providing benefits like specialization and augmentation. Derived classes inherit from base classes using the syntax class DerivedClassName : access-level BaseClassName { /* members */ };, with the access level determining visibility. While members can be inherited, constructors and destructors are not.

Access control for inherited members can be private (no derived class access), protected (access within derived and sub-classes), or public (access in any class). An example is the Point base class and 3D-Point derived class.

When a derived class object is created or destroyed, the base class's default constructor/destructor is automatically called. Specific base class constructors can be invoked using DerivedClassCon(...) : BaseClassCon(...) { ... }.

A derived class can have its own members and can override base class methods, meaning inherited objects calling an overridden method will execute the derived class version. This is demonstrated with Time (base) and ExtTime (derived) classes.

Polymorphism enables objects to be treated as instances of their parent class, allowing methods to be called without precise knowledge of their derived types. This is achieved using virtual functions, which facilitate dynamic or late binding. A base class method is declared virtual (e.g., virtual void Write()). For proper cleanup, destructors in classes with virtual functions should also be declared virtual.

Abstract classes cannot be instantiated directly and serve as blueprints. They contain pure virtual functions (e.g., virtual void draw() = 0;), which mandate that any derived class must provide an implementation for that function. For example, a Shape abstract class with a pure virtual draw() method requires Circle (derived from Shape) to implement its own draw() method.