Fixed #403: C++ Classes & Objects section bugged

This commit is contained in:
fechin committed 2023-04-06 13:45:07 +08:00
1 parent a751e11f87
commit 0036abeffd
1 file changed
+133 -5
+133 -5
View File
@@ -571,33 +571,161 @@ int main() {
}
```
C++ Classes & Objects
C++ Classes & Objects {.cols-2}
-----------------
### If statement
### Defining a Class
```cpp
class MyClass {
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
```
### If statement
### Creating an Object
```cpp
MyClass myObj; // Create an object of MyClass
myObj.myNum = 15; // Set the value of myNum to 15
myObj.myString = "Hello"; // Set the value of myString to "Hello"
cout << myObj.myNum << endl; // Output 15
cout << myObj.myString << endl; // Output "Hello"
```
### If statement
### Constructors
```cpp
class MyClass {
public:
int myNum;
string myString;
MyClass() { // Constructor
myNum = 0;
myString = "";
}
};
MyClass myObj; // Create an object of MyClass
cout << myObj.myNum << endl; // Output 0
cout << myObj.myString << endl; // Output ""
```
### If statement
### Destructors
```cpp
class MyClass {
public:
int myNum;
string myString;
MyClass() { // Constructor
myNum = 0;
myString = "";
}
~MyClass() { // Destructor
cout << "Object destroyed." << endl;
}
};
MyClass myObj; // Create an object of MyClass
// Code here...
// Object is destroyed automatically when the program exits the scope
```
### Class Methods
```cpp
class MyClass {
public:
int myNum;
string myString;
void myMethod() { // Method/function defined inside the class
cout << "Hello World!" << endl;
}
};
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
```
### Access Modifiers
```cpp
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
protected: // Protected access specifier
int z; // Protected attribute
};
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
myObj.z = 75; // Not allowed (protected)
```
### Getters and Setters
```cpp
class MyClass {
private:
int myNum;
public:
void setMyNum(int num) { // Setter
myNum = num;
}
int getMyNum() { // Getter
return myNum;
}
};
MyClass myObj;
myObj.setMyNum(15); // Set the value of myNum to 15
cout << myObj.getMyNum() << endl; // Output 15
```
### Inheritance
```cpp
class Vehicle {
public:
string brand = "Ford";
void honk() {
cout << "Tuut, tuut!" << endl;
}
};
class Car : public Vehicle {
public:
string model = "Mustang";
};
Car myCar;
myCar.honk(); // Output "Tuut, tuut!"
cout << myCar.brand + " " + myCar.model << endl; // Output "Ford Mustang"
```
C++ Preprocessor
------------