A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/cpp/c-classes-and-objects/ below:

C++ Classes and Objects - GeeksforGeeks

C++ Classes and Objects

Last Updated : 06 Aug, 2025

Try it on GfG Practice

In Object Oriented Programming, classes and objects are basic concepts of that are used to represent real-world concepts and entities.

Classes and Objects (Here Dog is the Class and Bobby is Object) C++ Classes

A class is a user-defined data type, which holds its own data members and member functions that can be accessed and used by creating an instance of that class. A C++ class is like a blueprint for an object.

Create a Class : A class must be defined before its use. C++ class is defined using the keyword class keyword as shown:

C++
class className {
access_specifier:

    // data member

    // member method
};

where,

Example:

C++
#include <iostream>
using namespace std;

// Class definition
class GfG {
public:
    // Data member
    int val;

    // Member function
    void show() {
        cout << "Value: " << val << endl;
    }
};

int main() {
    
   // Object will declareed here 
    return 0;
}

In the above, GfG class is created with a data member val and member function show(). Here, member function is defined inside the class, but they can also be just declared in the class and then defined outside using scope resolution operator ::

The above is called class definition or template.

C++ Objects

When a class is defined, only the specification (attributes and behaviour) for the object is defined. No memory is allocated to the class definition. To use the data and access functions defined in the class, we need to create its objects.

Objects are the actual entities that are created as an instance of a class. There can be as many objects of a class as desired. For example, in the above, we discussed the class of Cars. If we create an actual car based on the properties of the Car class, the car we made is the object of that class.

Create Object : Once the class is defined, we can create its object in the same way we declare the variables of any other inbuilt data type.

C++

This statement creates an object of className class.

Member Access : Members of the class can be accessed inside the class itself simply by using their assigned name.

To access them outside, the (.) dot operatoris used with the object of the class.

C++
obj.member1 // For data members
obj.member2(..) // For functions

There obj is the name of the object of the given class, member1 is data member and member2 is member function.

Local Class

Classes are generally declared in global scope and are accessible to every function or other classes once they are defined. But C++ also provides facility to define a class within a function. It is called local class in C++ and is only accessible in that function.

Nested Class

A nested class is a class defined within another enclosing class. As a member of the enclosing class, it has the same access rights as any other member. The members of the enclosing class do not have special access to the members of the nested class; the standard access rules apply.

This is the most common form:

class Outer {
public:
class Inner {
public:
void display() {
std::cout << "Inner class\n";
}
};
};
Enum Class

Enum classes in C++ are a safer and more organized way of using enums. They help to group related constants together while avoiding naming problems and ensuring better type safety.

Class vs Object

The following table lists the primary differences between the classes and objects in C++:

Class Object A blueprint or template for creating objects. An instance of a class with actual values. No memory is allocated for a class until an object is created. Memory is allocated when an object is created. Conceptual entity describing structure and behaviour. A real-world entity created from the class. Defines properties and functions common to all objects of that type. Stores specific data and manipulates it using class functions. Represents a general concept or type. Represents a specific instance of the class.

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4