Object and Class in C++ with Example

(Last Updated On: April 2, 2021)

 The relationship between class and object

Object And Class- I explained what an object is in my previous article. Every entity is an object. Some objects have the same structure and characteristic. For example, the first antiaircraft artillery company, the second antiaircraft artillery company, and the third antiaircraft artillery company are three different objects. But they belong to the same category Type; they have exactly the same structure and characteristics. The three targets of the first militia company, the second militia company, and the third militia company the types are also the same, but they are not the same as the types of antiaircraft artillery. Every object belongs to a specific type. The type of object in C++ is called a class. Classes represent the commonality and characteristics of a certain batch of objects. Already before Explanation: A class is an abstraction of an object, and an object is a concrete instance of the class. In C++, the status and role of classes Similar to structure. The relationship between class and object is equivalent to the relationship between structure type and structure variable. People first Declare a structure type, and then use it to define structure variables. The same structure type can define multiple Different structure variables.

In C++, it is also necessary to declare the type of a class first, and then use it to define several similar types Type of object. An object is a variable of a certain type. It is like building a house first to design drawings, and then press the drawings Paper built several houses of the same kind in different places. It can be said that a class is a template of an object, which is used to define an object. An abstract type. The class is abstract and does not take up memory, while the object is concrete and takes up storage space. At the beginning, figure out the object and The relationship between classes is very important.

Amazon Purchase Links:

Top Gaming Computers

Best Laptops

Best Graphic Cards

Portable Hard Drives

Best Keyboards

Best High Quality PC Mic

Computer Accessories

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

Declare the type of class

Classes are types created by users. If you want to use classes in the program, you must declare your own types according to your needs, or Use classes that others have created. The C++ standard itself does not provide the name, structure, and content of ready-made classes. How to declare (i.e. create) the type of a class in C++?

The following is the familiar declaration of a structure Type method:

The above declares a structure type named Student and defines two structure variables stud1 and stud2. You can see that it only includes data (variables), not operations. Now we declare a class:

It can be seen that the method of declaring a class is developed from the method of declaring a structure type. Line 1 (class Student) It is the (class head), which is composed of the keyword class and the class name Student. Class is the key that must be used when declaring the class. The keyword is equivalent to using struct when declaring the structure type. From the opening brace at the beginning of line 2 to the countdown The closing brace on line 2 is the class body. A pair of curly braces surrounds the class body. Class declaration the number ends.

In the class body is the class member list, which lists all the members in the class. You can see the addition number In addition to the data part, it also includes functions to manipulate these data. This embodies the encapsulation of data and operations together. The display in the class body is a function, through which the data in the object is operated, and the function is to output the data in the object Student ID, name and gender. Now the members encapsulated in class objects stud1 and stud2 are hidden from the outside world, and the outside world cannot call them. only The function display in this object can refer to data in the same object. That is, outside the class Cannot directly call the class Members in. This is of course “safe”, but how can the display function of the object stud1 be executed in the program? It cannot be started because it lacks an interface to the outside world, and the outside world cannot call member functions in the class, and is completely isolated from the outside world. What is the use of such a class? Obviously it has no practical effect. Therefore, all members of the class cannot be connected to the outside world Isolation is generally to conceal data and use member functions as an interface to the outside world. For example, a This command informs the object stud1 to execute the display function and output the relevant data of a certain student. You can change the declaration of the above class to

Now that it is declared that the display function is public, the outside world can call this function. If neither private nor public is specified in the definition of the class, the system defaults to private (No. This is the case with one class declaration). Summarizing the above declaration of class types, we can get its general form:

Private and public are called member access specifiers. Use them to declare each member’s Access properties. Declared as a private member, it can only be referenced by member functions in this class, and cannot be used outside the class. To be declared as a public member, either It is referenced by member functions in this class, and can also be referenced by other functions in the scope of the class. Lieutenant General Public is translated as “public”, that is, public and can be called by the outside world. About private (private) and public (public) Concept, you can make an analogy: in a family house, the living room is generally allowed to enter by any visitor, and the bedroom is one Generally, you do not want outsiders to enter, only your family members are allowed to enter. Generally, families will have such “open areas” and “inside Divisions. In this way, there will be a “small world” of privacy that is relatively isolated from the outside world and not disturbed by outsiders. In addition to private and public, there is also a member access qualifier protected (protected). The member declared protected is called a protected member, which cannot be accessed outside the class (this is similar to a private member), but Can be accessed by member functions of derived classes. When declaring a class type, the order of the members declared as private and the members declared as public is arbitrary.

The private part can appear first, or the public part can appear first. Not necessarily in a class body private and public parts, There can be only a private part or a public part. It has been explained before: if in the class body If neither the keyword private nor public is written in it, it defaults to private. In a class body, the keywords private and Public can appear multiple times, that is, a class body can contain multiple private and public parts. Each part The effective range is until the appearance of another access qualifier or the end of the class (the last closing brace). But in order to The program is clear, and such a habit should be cultivated; make each member access qualifier appear only once in the class definition body. In previous C++ programs, the private part often appeared first, followed by the public part, as shown above. right now Most C++ programs write the public part first, and put the private part at the back of the class body. This allows the user to Focusing on the members who can be called by the outside world makes the reader’s thinking clearer. Regardless of whether private or Is public, the role of the class is exactly the same.

In C++ programs, classes are often used. For user convenience, C++ compilation system often provides users with categories (But it is not part of the C++ language itself), it is equipped with common basic classes for users. Many users Put the classes that you or your unit often use in a special class library and call them directly when you need to use them, so as to reduce Programming workload.

Methods of defining objects

In the program segment listed above, the last line uses the established Student class to define the object. This method It is easy to understand. After being defined, Studl and stud2 become objects with the characteristics of the Student class. stud1 And stud2 are both Including the above data and functions respectively. In fact, just as there are many ways to define structure variables, there are many ways to define objects.

1.Declare the class type

first, then define the object We used this method earlier, such as

Student stud1, stud2;     //Student is the declared class type

In C++, after the class type is declared, there are two forms of defining objects.

  • class name object name;

class Student stud1, stud2;

Combine class and Student as a class name to define objects.

  • Class name object name;

Such as Student stud1, stud2;

Define the object directly with the class name. These two methods are equivalent. The first method is inherited from the C language, the second This method is a characteristic of C++, obviously the second method is more convenient and convenient to use.

  1. Define the object while declaring the class

While defining the Student class, two objects of the Student class are defined.

  1. No class name appears, directly define the object

 Directly defining objects is legal and allowed in C++, but it is rarely used and is not recommended. Because In object-oriented programming and C++ programs, the declaration of the class and the use of the class are separated, and the class is not just a program For services, people often encapsulate some commonly used functions into classes and put them in class libraries. Therefore, in the actual program development, one Generally, the first method of the above 3 methods is used. In a small program or when the declared class is only used in this program, You can also use the second type method. The sample questions in this book are basically small sample programs, and in order to be intuitive and easy to understand, Put the declaration of the class and the definition object in a similar position, so the second method is often used. When defining an object, the compiler system will allocate storage space for the object to store the members of the object.

 Similarities and differences between classes and structure types

After C++ adds the class type, it still retains the structure type (Struct) and extends its functions. C++ allows to declare a class with struct. For example, the class type declared with the keyword class can be changed to use Keywords struct:

Someone will naturally ask: What is the difference between the two? Why do you need to do this, the function is repeated? This is due to C++ A principle set during the language design: C++ must be compatible with C, so that a large number of C programs written in the past cannot Add and modify to use in C++ environment. It is conceivable that if C++ is designed from scratch (rather than based on C Basis), it is very likely that the structure data type will not be provided, because the class type already includes all the structure types Function, and the function is stronger, more in line with the requirements of object-oriented programming. In order to make the structure type also have encapsulated Features, C++ does not simply inherit the structure of C, but makes it also have the characteristics of a class, so that it can be used for object-oriented programming. The structure type declared with struct is actually a class. However, the class declared with struct is different. The class declared with struct, if it is The member does not make a private or public declaration, and the system sets it as public by default. If you want to specify the private If there are members and public members, use private or public as an explicit declaration. And the class defined by class, if not Private or public declaration, the system sets its members as private by default, and you can use it yourself when you need it Explicitly declare changes.

So, when should I use struct and when should I use class? If you want the members to be public, use struct is more convenient. If you want some members to be private, use class. We recommend to use class to create Class, write a program that fully reflects the C++ style.

Recommended For You

About the Author: Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply