0% found this document useful (0 votes)
18 views

Class Objects

C++ classes define user-defined data types that can contain attributes like variables and methods like functions. A class acts as a blueprint for creating objects that can access attributes and methods. Constructors are special methods that are called when an object is created.

Uploaded by

floweryyeeaarr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Class Objects

C++ classes define user-defined data types that can contain attributes like variables and methods like functions. A class acts as a blueprint for creating objects that can access attributes and methods. Constructors are special methods that are called when an object is created.

Uploaded by

floweryyeeaarr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C++ Classes/Objects

C++ is an object-oriented programming language.

Everything in C++ is associated with classes and objects, along with its attributes and methods.
For example: in real life, a car is an object. The car has attributes, such as weight and color,
and methods, such as drive and brake. Attributes and methods are
basically variables and functions that belongs to the class. These are often referred to as "class
members".

A class is a user-defined data type that we can use in our program, and it works as an object
constructor, or a "blueprint" for creating objects.

Example

Create a class called "MyClass":

class MyClass { // The class


public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};

Example explained

• The class keyword is used to create a class called MyClass.


• The public keyword is an access specifier, which specifies that members (attributes and
methods) of the class are accessible from outside the class. You will learn more
about access specifiers later.
• Inside the class, there is an integer variable myNum and a string variable myString. When
variables are declared within a class, they are called attributes.
• At last, end the class definition with a semicolon ;.

Create an Object
• In C++, an object is created from a class. We have already created the class
named MyClass, so now we can use this to create objects.
• To create an object of MyClass, specify the class name, followed by the object name.
• To access the class attributes (myNum and myString), use the dot syntax (.) on the object:

Example: Create an object called "myObj" and


access the attributes:
class MyClass { // The class
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};

int main() {
MyClass myObj; // Create an object of MyClass

// Access attributes and set values


myObj.myNum = 15;
myObj.myString = "Some text";

// Print attribute values


cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
}

Class with Multiple Objects:

Example
// Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};

int main() {
// Create an object of Car
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;

// Create another object of Car


Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;

// Print attribute values


cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}

Class Methods

Methods are functions that belongs to the class.

There are two ways to define functions that belongs to a class:

• Inside class definition


• Outside class definition

In the following example, we define a function inside the class, and we name it "myMethod".

Note: You access methods just like you access attributes; by creating an object of the class and
using the dot syntax (.):

Inside Example
class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function defined inside the class
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
To define a function outside the class definition, you have to declare it inside the class and then
define it outside of the class. This is done by specifying the name of the class, followed the scope
resolution :: operator, followed by the name of the function:
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};

// Method/function definition outside the class


void MyClass::myMethod() {
cout << "Hello World!";
}

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}

Constructors
A constructor in C++ is a special method that is automatically called when an object of a class is
created. To create a constructor, use the same name as the class, followed by parentheses ():

You might also like