1 Differences Between struct and class
When declaring members in a struct, their access specifier defaults to public if not explicitly defined.
Conversely, when declaring members in a class, their access specifier defaults to private if not explicitly defined.
2 Initialization Lists
Classes in C++ can initialize their data members through two primary methods in constructors:
Method 1: Assignment within the constructor body
Method 2: Using initialization lists
Key differences: Assignment initialization inside the constructor body occurs after the constructor begins execution, whereas initialization lists perform the initialiaztion at the moment of object construction. When dealing with objects that require specific initialization, using initialization lists is essential to avoid potential compilation errors. Therefore, it's recommended to always use initialization lists for member initialization.
Example
(a) Error when using assignment initialization in constructor
1 class Robot
2 {
3 public:
4 Robot(int id)
5 {
6 }
7 ~Robot()
8 {
9 }
10 };
11
12 class Machine
13 {
14 private:
15 string model;
16 string serial;
17 int version;
18 Robot controller;
19 public:
20 Machine()
21 {
22 model = "";
23 serial = "";
24 version = 0;
25 controller = 0;
26 }
27 ~Machine()
28 {
29 }
30 };
Error message: error: no matching function for call to 'Robot::Robot()'
(b) Problem resolved with initialization list
1 class Robot
2 {
3 public:
4 Robot(int id)
5 {
6 }
7 ~Robot()
8 {
9 }
10 };
11
12 class Machine
13 {
14 private:
15 string model;
16 string serial;
17 int version;
18 Robot controller;
19 public:
20 Machine():controller(0), model(""), serial(""), version(0)
21 {
22 }
23 ~Machine()
24 {
25 }
26 };
3 Composition and Inheritance
3.1 Composition
Composition involves including objects of other classes as member variables within a class.
Example
1 /* Composition example */
2 class Engine
3 {
4 private:
5 string type;
6 string power;
7 public:
8 Engine(int id):type(""), power("")
9 {
10
11 }
12 ~Engine()
13 {
14 }
15 };
16
17 class Vehicle
18 {
19 private:
20 Engine powerplant;
21 public:
22 Vehicle(int id):powerplant(0)
23 {
24 }
25 ~Vehicle()
26 {
27 }
28 };
3.2 Inheritance
Inheritance involves creating a new class that derives properties and behaviors from an existing class.
Example
1 /* Inheritance example */
2 class Vehicle
3 {
4 private:
5 string brand;
6 string model;
7 public:
8 Vehicle(int id):brand(""), model("")
9 {
10 }
11 ~Vehicle()
12 {
13 }
14
15 void SetBrand(string brand_)
16 {
17 brand = brand_;
18 }
19 };
20
21 class Car : public Vehicle
22 {
23 public:
24 Car(int id):Vehicle(0)
25 {
26 }
27 ~Car()
28 {
29 }
30 };
Inheritance Access Permissions
public The derived class can directly access and modify all public members of the base class.
private The derived class inherits all base class members but cannot directly access or modify them; it must use public interfaces provided by the base class.
protected For derived classes, access is similar to public members, but for external objects, access is restricted like private members.