C++ Introductory Tutorial||C++ Class & Object||C++ Inheritance

C++ classes & objects

C++ classes & objects

C++ adds object-oriented programming to the C language, and C++ supports object-oriented programming. Classes are a core feature of C++ and are often referred to as user-defined types.

A class is used to specify the form of an object, which contains data representation and methods for manipulating the data. The data and methods in a class are called members of the class. Functions in a class are called members of the class.

C++ class definition

Defining a class is essentially a blueprint for defining a data type. This doesn't actually define any data, but it defines what the name of the class means, that is, it defines what an object of the class contains, and what operations can be performed on this object.

A class definition begins with the keyword  class  followed by the name of the class. The body of the class is enclosed in a pair of curly braces. A class definition must be followed by a semicolon or a list of declarations. For example, we define the Box data type using the keyword  class  as follows:

class Box
{
   public:
      double length;   // Length of a box
      double breadth;  // Breadth of a box
      double height;   // Height of a box
};

The keyword  public  determines the access attributes of class members. Within class object scope, public members are accessible outside the class. You can also specify the members of the class as  private  or  protected , which we will explain later.

Define a C++ object

Classes provide the blueprint for objects, so basically, objects are created based on classes. Objects of classes are declared just like variables of primitive types. The following statement declares two objects of class Box:

Box Box1; // Declare Box1, type Box Box 
Box2; // Declare Box2, type Box

Objects Box1 and Box2 have their respective data members.

access data members

Public data members of objects of a class can be accessed using the direct member access operator (.). To better understand these concepts, let's try the following example:

#include <iostream>

using namespace std;

class Box
{
   public:
      double length;   // 长度
      double breadth;  // 宽度
      double height;   // 高度
};

int main( )
{
   Box Box1;        // 声明 Box1,类型为 Box
   Box Box2;        // 声明 Box2,类型为 Box
   double volume = 0.0;     // 用于存储体积
 
   // box 1 详述
   Box1.height = 5.0; 
   Box1.length = 6.0; 
   Box1.breadth = 7.0;

   // box 2 详述
   Box2.height = 10.0;
   Box2.length = 12.0;
   Box2.breadth = 13.0;

   // box 1 的体积
   volume = Box1.height * Box1.length * Box1.breadth;
   cout << "Box1 的体积:" << volume <<endl;

   // box 2 的体积
   volume = Box2.height * Box2.length * Box2.breadth;
   cout << "Box2 的体积:" << volume <<endl;
   return 0;
}

When the above code is compiled and executed, it produces the following result:

Volume of Box1: 210 
Volume of Box2: 1560

Note that private and protected members cannot be accessed directly using the direct member access operator (.). We will learn how to access private and protected members in subsequent tutorials.

Class & Object Details

So far, we've had a basic understanding of C++ classes and objects. The following list also lists some other C++ class and object-related concepts, you can click on the corresponding link to learn.

concept describe
class member function Member functions of a class are those functions whose definition and prototype are written inside the class definition, just like other variables in the class definition.
class access modifier Class members can be defined as public, private or protected. By default it is defined as private.
Constructor & Destructor A class constructor is a special function that is called when a new object is created. A class's destructor is also a special function that is called when an object created is deleted.
C++ copy constructor A copy constructor is a special constructor that uses a previously created object in the same class to initialize a newly created object when creating an object.
C++ friend function Friend functions can access private and protected members of a class.
C++ intrinsics By inlining a function, the compiler attempts to expand the code in the body of the function at the point where the function is called.
The this pointer in C++ Every object has a special pointer  this , which points to the object itself.
Pointer to class in C++ A pointer to a class behaves like a pointer to a structure. In fact, a class can be thought of as a structure with functions.
Static members of C++ classes Both data members and function members of a class can be declared static.

C++ inheritance

C++ inheritance

One of the most important concepts in object-oriented programming is inheritance. Inheritance allows us to define a class in terms of another class, which makes it easier to create and maintain an application. In doing so, the effect of reusing code functions and improving execution time is also achieved.

When creating a class, you don't need to rewrite new data members and member functions, you only need to specify that the new class inherits the members of an existing class. This existing class is called the base class , and the newly created class is called the derived class .

Inheritance represents  the is a  relationship. For example, a mammal is an animal, a dog is a mammal, therefore, a dog is an animal, and so on.

Base Class & Derived Class

A class can be derived from multiple classes, which means it can inherit data and functions from multiple base classes. To define a derived class, we use a class-derived-list to specify the base class. A class derivation list is named after one or more base classes and has the following form:

class derived-class: access-specifier base-class

Among them, the access modifier access-specifier is  one of public, protected  or  private  , and base-class is the name of a class defined before. If the access-specifier is not used, it defaults to private.

Suppose there is a base class  Shape , and Rectangle  is its derived class, as follows:

#include <iostream>
 
using namespace std;

// 基类
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

// 派生类
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};

int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);

   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;

   return 0;
} 

When the above code is compiled and executed, it produces the following result:

Total area: 35

Access Control and Inheritance

Derived classes can access all non-private members of the base class. Therefore, if the base class members do not want to be accessed by the member functions of the derived class, they should be declared as private in the base class.

We can summarize the different access types based on access rights as follows:

access public protected private
the same class yes yes yes
Derived class yes yes no
external class yes no no

A derived class inherits all base class methods with the following exceptions:

  • Base class constructor, destructor, and copy constructor.
  • Overloaded operators for the base class.
  • Friend function of the base class.

inheritance type

When a class is derived from a base class, the base class can be inherited as  public, protected  or  private  . The inheritance type is specified by the access modifier access-specifier explained above.

We rarely use  protected  or  private  inheritance, usually  public  inheritance. When using different types of inheritance, there are a few rules to follow:

  • Public inheritance (public): When a class is derived from a public base class, the public members of the base class are also public members of the derived class, and the protected members of the base class are also protected members of the derived class . The private members of the base class cannot be directly inherited by the derived class. access, but can be accessed by calling public and protected members of the base class.
  • Protected Inheritance:  When a class is derived from a protected base class, the public and protected members of the base class become protected members of the derived class.
  • Private inheritance (private): When a class is derived from a private base class, the public and protected members of the base class become private members of the derived class.

multiple inheritance

Multiple inheritance means that a subclass can have multiple parent classes, and it inherits the characteristics of multiple parent classes.

A C++ class can inherit members from multiple classes, the syntax is as follows:

class <derived class name>:<inheritance mode 1> <base class name 1>, <inheritance mode 2> <base class name 2>,... { <derived class body 
> 
} 
;

Among them, the access modifier access is   one of public, protected  or  private , and is used to modify each base class, and each base class is separated by a comma, as shown above. Now let's look at the following example:

#include <iostream>
 
using namespace std;

// 基类 Shape
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

// 基类 PaintCost
class PaintCost 
{
   public:
      int getCost(int area)
      {
         return area * 70;
      }
};

// 派生类
class Rectangle: public Shape, public PaintCost
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};

int main(void)
{
   Rectangle Rect;
   int area;
 
   Rect.setWidth(5);
   Rect.setHeight(7);

   area = Rect.getArea();
   
   // 输出对象的面积
   cout << "Total area: " << Rect.getArea() << endl;

   // 输出总花费
   cout << "Total paint cost: $" << Rect.getCost(area) << endl;

   return 0;
}

When the above code is compiled and executed, it produces the following result:

Total area: 35
Total paint cost: $2450

Guess you like

Origin blog.csdn.net/m0_69824302/article/details/130128567