-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcpp cheat sheet
66 lines (53 loc) · 2.69 KB
/
cpp cheat sheet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Certainly! Here's a cheat sheet summarizing the key concepts related to classes and objects in C++:
Classes and Objects:
- A class is a user-defined data type that encapsulates data members (variables) and member functions (methods) into a single unit.
- An object is an instance of a class. It represents a specific occurrence of the class and can be used to access its members.
Class Definition:
```cpp
class ClassName {
// Data members (variables)
// Member functions (methods)
};
```
Access Specifiers:
- `public`: Members are accessible from anywhere.
- `private`: Members are only accessible from within the class.
Member Functions:
- Member functions are functions defined inside a class.
- They can be declared and defined inside the class declaration or separately outside the class.
- They can access the class's data members and other member functions.
- Member functions can be classified as `public`, `private`, or `protected` based on their access level.
Data Members:
- Data members are variables declared inside a class.
- They can be `public`, `private`, or `protected` based on their access level.
- Data members represent the state or characteristics of an object.
Constructor:
- A constructor is a special member function with the same name as the class.
- It is automatically invoked when an object of the class is created.
- Constructors are used to initialize the object's data members.
Destructor:
- A destructor is a special member function with the same name as the class preceded by a tilde (`~`).
- It is automatically invoked when an object is destroyed or goes out of scope.
- Destructors are used to release resources or perform cleanup operations.
Member Function Definition:
```cpp
return_type ClassName::functionName(parameters) {
// Function body
}
```
Creating Objects:
```cpp
ClassName objectName; // Default constructor
ClassName objectName(value); // Parameterized constructor
ClassName* ptr = new ClassName(); // Dynamic object creation
```
Accessing Members:
- Dot operator (`.`) is used to access members of an object.
- Arrow operator (`->`) is used to access members through a pointer to an object.
Friend Function:
- A friend function is a non-member function that has access to the private and protected members of a class.
- It can be declared inside the class and defined outside the class.
Friend Class:
- A friend class is a class that has access to the private and protected members of another class.
- It is declared using the `friend` keyword inside the class that grants access.
This cheat sheet covers the fundamental concepts related to classes and objects in C++. Understanding and applying these concepts will help you work with object-oriented programming in C++.