Chapter 4 of C++ Programming: Classes

Chapter 4 Classes unset4-1 Explain the role of public and private. What are the differences between public and private members?unsetunset Answer: Public members are declared with the <span>public</span> keyword, defining the external interface of the class, allowing external objects to access them directly; private members are declared with the <span>private</span> keyword, allowing access only by … Read more

C++ Inheritance and Constructors: A Beginner’s Guide from a Theoretical Perspective

Inheritance 1. What is the concept? Inheritance is essentially an extension of an existing class’s characteristics, adding new functionalities. The new class created is called a derived class, while the class being inherited from is called the base class. In fact, inheritance is primarily a form of reuse at the class hierarchy level. For example, … Read more

Introduction to C++: Object-Oriented Programming – Classes

Object-Oriented Programming – Classes Procedural Programming: A process-centered programming paradigm Object-Oriented Programming (OOP): A programming paradigm centered around objects Encapsulation, Inheritance, Polymorphism Basic Concepts of Classes and Objects A class encapsulates data and methods that share common characteristics, distinguishing them by access levels, to describe or abstract an entity Access levels in a class are: … Read more

C++ Programming Tips: Avoid Implicit Type Conversions

C++ Programming Tips: Avoid Implicit Type Conversions

C++ performs “implicit conversions” between different types, such as silently converting char to int or int to char. “Implicit conversions” allow us to successfully provide a short or int to a function that requires a double.The above “implicit conversions” are provided by the language, and we cannot refuse them. However, C++ allows us to provide … Read more

C++ Programming Tips: Avoid Providing Default Empty Constructors Unless Necessary

C++ Programming Tips: Avoid Providing Default Empty Constructors Unless Necessary

1. Default empty constructors are not necessaryCompared to other constructors, the characteristic of a default empty constructor is that it can construct an object without requiring any parameters.However, we cannot always create objects this way in practice. For example, when creating an “ID card” object, the ID number is always required. Therefore, providing a default … Read more

Introduction to Object-Oriented Programming in Embedded C++

Introduction to Object-Oriented Programming in Embedded C++

Follow our official account to keep receiving embedded knowledge! 1. Comparison of Object-Oriented and Procedure-Oriented Programming Imagine you want to build a house. Procedure-Oriented: It’s like you are doing everything yourself. Your thinking is linear: “First, I will move bricks -> then mix cement -> next, build walls -> finally, put on the roof.” You … Read more

C++ Programming Guidelines – Constructors, Assignment, and Destructors

C++ Programming Guidelines - Constructors, Assignment, and Destructors

01 Classes containing member variables must define a constructor or a default constructor. Note: If a class has member variables and does not define a constructor or a default constructor, the compiler will automatically generate a constructor, but the compiler-generated constructor will not initialize the member variables, leaving the object in an uncertain state.Exception: If … Read more

C++ Basics: The explicit Keyword

C++ Basics: The explicit Keyword

1. What is explicit? explicit is a C++ keyword It modifies constructors and type conversion functions Purpose: to prohibit implicit type conversions 2. What is the use of explicit?First, let’s look at the following example: class A{public: A(int x) { }}; void foo(A a) { } foo(10); // Valid, int implicitly converts to A By … Read more

In-Depth Analysis of Virtual Functions and Constructors/Destructors in C++

In-Depth Analysis of Virtual Functions and Constructors/Destructors in C++

In-Depth Analysis of Virtual Functions and Constructors/Destructors in C++ Exploring key techniques and underlying implementations in object-oriented programming In C++ object-oriented programming, the virtual function mechanism is central to achieving polymorphism. However, when virtual functions are used in conjunction with special member functions (especially constructors and destructors), there are many details and considerations that require … Read more

Practical Guide to Rust Constructors: From Beginner to Expert

Practical Guide to Rust Constructors: From Beginner to Expert

This article is from https://substack.com/@cuongleqq/p-171247647 Every serious Rust developer should master constructor patterns to avoid looking like a novice. I used to think that <span>new</span> was the constructor in Rust. I was wrong; it is just a conventional method for creating new struct instances. Later, I encountered <span>with_capacity</span>, <span>Default</span>, <span>TryFrom</span>, and the builder pattern (which … Read more