- C++ Basics
- C++ Introduction
- C++ Installation
- C++ Syntax
- C++ Hello World
- C++ Comments
- C++ Variables
- C++ Data Types
- C++ Constants
- C++ Type Casting
- C++ Inline
- C++ File Inclusion
- C++ Date & Time
- C++ Return Types
- C++ Object Oriented
- C++ Classes
- C++ Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Exceptions
- C++ Advanced
- C++ Conditions
- C++ Loops
- C++ Functions
- C++ Structures
- C++ Enums
- C++ References
- C++ Pointers
- C++ Data Structures
- C++ Libs
- C++ Data Structures
- C++ Arrays
- C++ Vectors
- C++ Lists
- C++ Linked List
- C++ Deque
- C++ Stacks
- C++ Queues
- C++ Priority Queues
- C++ Sets
- C++ Maps
- C++ Unordered Sets and Maps
- C++ Graphs
CPP Objects
In C++, objects are instances of classes, which are user-defined types. A class defines the properties (attributes) and behaviors (methods) that the object can have. When you create an object, you're essentially creating a specific instance of that class, with its own values for the properties and access to the behaviors.
Basic Structure of C++ Classes and Objects
Here’s a simple example:
cpp#include
using namespace std;
// Define a class
class Car {
public:
// Attributes (member variables)
string brand;
string model;
int year;
// Constructor
Car(string b, string m, int y) {
brand = b;
model = m;
year = y;
}
// Method (member function)
void displayInfo() {
cout << "Brand: " << brand << ", Model: " << model << ", Year: " << year << endl;
}
};
int main() {
// Create objects of the Car class
Car car1("Toyota", "Corolla", 2020);
Car car2("Honda", "Civic", 2022);
// Access object properties and methods
car1.displayInfo();
car2.displayInfo();
return 0;
}
Key Concepts
- Class: A blueprint for creating objects (instances).
- Object: An instance of a class.
- Constructor: A special method used to initialize objects.
- Methods: Functions defined inside a class that can operate on objects.