0% found this document useful (0 votes)
18 views7 pages

1 Class and Object

This document provides an overview of classes and objects in C++, explaining that a class is a user-defined data type that serves as a blueprint for creating objects. It details how to define a class, create objects from it, and access attributes and methods using dot syntax. Additionally, it describes how to define member functions both inside and outside of the class definition, illustrating these concepts with code examples.

Uploaded by

Sonu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

1 Class and Object

This document provides an overview of classes and objects in C++, explaining that a class is a user-defined data type that serves as a blueprint for creating objects. It details how to define a class, create objects from it, and access attributes and methods using dot syntax. Additionally, it describes how to define member functions both inside and outside of the class definition, illustrating these concepts with code examples.

Uploaded by

Sonu Saini
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ Classes and Objects

Class in C++ is the building block that leads to Object-Oriented programming.


It is a user-defined data type, which holds its own data members and member
functions, which can be accessed and used by creating an instance of that class.
A C++ class is like a blueprint for an object.
For example: in real life, Consider the Class of Cars. There may be many cars with
different names and brands. A car is an object. The car has attributes, such as
weight, color, speed limit, mileage, and methods, such as drive and brake.
In the above example, the data member will be weight, color, speed
limit, mileage, etc, and member functions can be applying brakes, increasing
speed, etc.

An Object is an instance of a Class. When a class is defined, no memory is


allocated but when it is instantiated (i.e. an object is created) memory is
allocated.

Create a Class
A class is defined in C++ using the keyword class followed by the name of the
class. The body of the class is defined inside the curly brackets and terminated
by a semicolon at the end.

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.
 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 1 -- 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";

Example 2
// Print attribute values
cout << myObj.myNum << "\n";
cout << myObj.myString;
return 0;
#include <iostream>
}
#include <string>
using namespace std;
Output:-- 15
Kush

Example -3 (Multiple Objects)


#include <iostream>
#include <string>
using namespace std;
C++ Class Methods
Class is a blueprint of an object, which has data members and member functions
also known as methods. A method is a procedure or function in the oops concept.
A method is a function that belongs to a class.
There are two ways to define a procedure or function that belongs to a class:
 Inside Class Definition
 Outside Class Definition

1. Inside Class Definition
The member function is defined inside the class definition it can be defined
directly. Similar to accessing a data member in the class we can also access the
public member functions through the class object using the dot operator (.).

Syntax:--

class class_name{
class class_name{
public:
public:
return_type Method_name()
return_type Method_name()
// method inside class
// method inside class definition
definition
{
{
// body of member function
}
// body of member function

};}
};

Example
#include <iostream>
#include <string>
using namespace std;

class btech // the class


{
public: // Access specifier
Output --- cse 15 kush
2. Outside Class Definition

The member function is defined outside the class definition it can be defined
using the scope resolution operator. Similar to accessing a data member in the
class we can also access the public member functions through the class object
using the dot operator (.).

Syntax:
class Class_name{
public:

return_type Method_name(); // method outside class definition

};

// Outside the Class using scope resolution operator


Example:

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;
}

You might also like