English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, you will learn about objects and classes in C ++Objects and classes are used in programming.
C ++It is a multiparadigm programming language. This means that it supports different programming styles.
A popular method to solve programming problems is to create objects, which is known as the object-oriented programming style.
C ++Supports object-oriented (OO) programming style, which allows you to break complex problems into smaller sets by creating objects.
An object is just a collection of data and functions that operate on that data.
In C ++Before creating an object in
A class is the blueprint of an object.
We can view a class as a blueprint (prototype) of a house. It contains all the details about the floor, doors, windows, etc. Based on these descriptions, we build houses. And the house is an object.
Since many houses can be made with the same description, we can create many objects based on one class.
In C ++In it, a class is defined using the keyword class followed by the class name.
The body of the class is defined within the curly braces and ends withSemicolonend.
class className { // some data //some functions };
class Test { private: int data1; float data2; public: void function1() { data1 = 2; } float function2() { data2 = 3.5; return data2; } };
Here, we define a class named Test.
This class has two data members: data1In this program, two data members data2and two member functions: function1() and function2()
In the above example, you may have noticed two keywords: private and public.
The private keyword makes data and functions private. Only private data and functions can be accessed from within the same class.
The public keyword makes data and functions public. Data and functions can be accessed outside the class.
Here, data1In this program, two data members data2is a private member, while function1() and function2() are public members.
If an attempt is made to access private data from outside the class, the compiler will raise an error.In OOPThis feature is called data hiding.
When defining a class, only the specification of the object is defined; no memory or storage space is allocated.
To use the data and access functions defined in the class, an object needs to be created.
className objectVariableName;
You can create an object of the Test class using the following method (as defined in the above example):
class Test { private: int data1; float data2; public: void function1() { data1 = 2; } float function2() { data2 = 3.5; return data2; } }; int main() { Test o1, o2; }
Here, two objects of the Test class are created o1and o2.
In the above Test class, data1In this program, two data members data2are data members, while function1() and function2() is a member function.
You can use to access data members and member functions. The (.) operator. For example,
o2.function1();
This will create an object o in the Test class2Call function1() function.
Similarly, data members can be accessed in the following way:
o1.data2 = 5.5;
It is important to note that private members can only be accessed from within the class.
Therefore, you can use o2.function1; Any function or class in the above example.1.data2 = 5.5; It should always be in the class Test.
// Used to illustrate C ++Program that demonstrates the operation of objects and classes #include <iostream> using namespace std; class Test { private: int data1; float data2; public: void insertIntegerData(int d) { data1 = d; cout << "Number: " << data1; } float insertFloatData() { cout << "\nInput data: "; cin >> data2; return data2; } }; int main() { Test o1, o2; float secondDataOfObject2; o1.insertIntegerData(12); secondDataOfObject2 = o2.insertFloatData(); cout << "You have entered " << secondDataOfObject2; return 0; }
Output result
Number: 12 Input data: 23.3 You have entered 23.3
are defined in the Test class.1In this program, two data members data2and data
and two member functions insertIntegerData() and insertFloatData().1and o2.
Use the following method to declare two objects of the same class o1The object o calls the insertIntegerData() function:
o1.insertIntegerData(12);
This will set the object o1of data1The value is set to12.
Then, call the object o2The insertFloatData() function, and use the following method to store the return value of the function in the variable secondDataOfObject2in:
secondDataOfObject2 = o2.insertFloatData();
In this program, o1of data2and o2of data1Not used and contains garbage values.
You should also check the following topics to learn more about objects and classes: