-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP&object-classes.cpp
More file actions
86 lines (69 loc) · 2.89 KB
/
Copy pathOOP&object-classes.cpp
File metadata and controls
86 lines (69 loc) · 2.89 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <string>
using namespace std;
/*
===================================================================
1. CORE CONCEPT: WHAT IS OOP? (Object-Oriented Programming)
===================================================================
- OOP is a programming paradigm centered around "objects" that contain
DATA (attributes) and FUNCTIONS (methods) operating on that data.
- Advantages of OOP:
1. Provides a clear structure for programs.
2. DRY (Don't Repeat Yourself) principle -> Code is easy to maintain,
reuse, and debug.
3. Faster execution and shorter development time for full applications.
- Procedural vs. OOP:
* Procedural: Focuses on writing functions/procedures that act on data.
* OOP : Focuses on creating objects that combine both data and methods.
===================================================================
2. CLASS vs. OBJECT
===================================================================
- Class : A user-defined data type that acts as a "blueprint" for objects.
- Object : An instance of a class that inherits its attributes and methods.
(e.g., Class: 'Car' | Objects: 'Volvo', 'Audi', 'Toyota')
*/
// ===================================================================
// 3. CREATING A CLASS
// ===================================================================
// Use the `class` keyword to define a class.
class Car {
public: // Access specifier: Members below are accessible from outside
string brand; // Attribute (variable inside a class)
string model; // Attribute
int year; // Attribute
}; // Don't forget to end the class definition with a semicolon (;)
class MyClass {
public:
int myNum;
string myString;
};
// ===================================================================
// MAIN FUNCTION & CREATING OBJECTS
// ===================================================================
int main() {
// --- Single Object Example ---
cout << "=== 1. SINGLE OBJECT ===" << endl;
MyClass myObj; // Create an object of MyClass
// Access attributes using the dot syntax (.)
myObj.myNum = 15;
myObj.myString = "Some text";
cout << "Number: " << myObj.myNum << endl;
cout << "Text : " << myObj.myString << endl;
// --- Multiple Objects Example ---
cout << "\n=== 2. MULTIPLE OBJECTS ===" << endl;
// Object 1
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
// Object 2 (Created from the same 'Car' class)
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
// Output Object 1 values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << endl;
// Output Object 2 values
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << endl;
return 0;
}