Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

C++ Classes & Objects

C++ Classes & Objects

C++ adds object-oriented programming on top of C; C++ supports object-oriented programming. The class is a core feature of C++, often called a user-defined type.

A class specifies the form of an object; it is a user-defined data type that combines data and functions in a package. Data in a class is called member variables, and functions are called member functions. A class can be viewed as a template used to create multiple objects with the same attributes and behavior.

C++ Class Definition

Defining a class requires the keyword class, followed by the class name; the class body is enclosed in a pair of curly braces and contains the class's member variables and member functions.

Defining a class is essentially defining a blueprint for a data type: it defines what the class's objects contain and which operations can be performed on those objects.

In the following example we use the keyword class to define the Box data type, which contains three member variables: length, breadth, and height:

class

Box

{

public

:

double

length

;

// Length of the box

double

breadth

;

// Width of the box

double

height

;

// Height of the box

}

;

The keyword public determines the access attributes of class members. Within the class object's scope, public members are accessible from outside the class. You can also specify class members as private or protected, which we will explain later.

Defining C++ Objects

A class provides the blueprint for objects, so basically objects are created from classes. Declaring an object of a class is like declaring a variable of a basic type. The following statement declares two objects of class Box:

Box

Box1

;

// declare Box1 of type Box

Box

Box2

;

// declare Box2 of type Box

Objects Box1 and Box2 each have their own data members.

Accessing Data Members

The public data members of a class object can be accessed using the direct member access operator .

To better understand these concepts, let's try the example below:

Example

#include

<
iostream
using
namespace
std

;

class

Box

{

public

:

double

length

;

// length

double

breadth

;

// width

double

height

;

// height

// Member function declaration

double

get

(

void

)

;

void

set

(

double

len

,

double

bre

,

double

hei

)

;

}

;

// Member function definition

double

Box

::

get

(

void

)

{

return

length

breadth

height

;

}

void

Box

::

set

(

double

len

,

double

bre

,

double

hei

)

{

length

=

len

;

breadth

=

bre

;

height

=

hei

;

}

int

main

(

)

{

Box

Box1

;

// declare Box1 of type Box

Box

Box2

;

// declare Box2 of type Box

Box

Box3

;

// declare Box3 of type Box

double

volume

=

0

.0

;

// Used to store volume

// box 1 details

Box1

.

height

=

5

.0

;

Box1

.

length

=

6

.0

;

Box1

.

breadth

=

7

.0

;

// box 2 details

Box2

.

height

=

10

.0

;

Box2

.

length

=

12

.0

;

Box2

.

breadth

=

13

.0

;

// Volume of box 1

volume

=

Box1

.

height

Box1

.

length

Box1

.

breadth

;

cout

<<

"

Volume of Box1:

"

<<

volume

<<

endl

;

// Volume of box 2

volume

=

Box2

.

height

Box2

.

length

Box2

.

breadth

;

cout

<<

"

Volume of Box2:

"

<<

volume

<<

endl

;

// box 3 details

Box3

.

set

(

16

.0

,

8

.0

,

12

.0

)

;

volume

=

Box3

.

get

(

)

;

cout

<<

"

Volume of Box3:

"

<<

volume

<<

endl

;

return

0

;

}

When the code above is compiled and executed, it produces the following result:


Box1 的体积:210
Box2 的体积:1560
Box3 的体积:1536

Note that private and protected members cannot be accessed directly using the direct member access operator (.). We will learn how to access private and protected members in later tutorials.

Classes & Objects in Detail

So far, we have a basic understanding of C++ classes and objects. The list below also lists other C++ class- and object-related concepts; click the corresponding links to learn more.

Concept Description
Class Member Functions A class member function is a function whose definition and prototype are written inside the class definition, just like other variables in the class definition.
Class Access Modifiers Class members can be defined as public, private, or protected. By default, they are defined as private.
Constructors & Destructors A class's constructor is a special function called when a new object is created. A class's destructor is also a special function, called when the created object is deleted.
C++ Copy Constructor A copy constructor is a special constructor that, when creating an object, uses a previously created object of the same class to initialize the newly created object.
C++ Friend Functions Friend functionscan access the class's private and protected members.
C++ Inline Functions With inline functions, the compiler attempts to expand the function body's code at the call site.
The this Pointer in C++ Each object has a special pointer thisthat points to the object itself.
Pointers to Classes in C++ A pointer to a class works like a pointer to a structure. In fact, a class can be viewed as a structure with functions.
Static Members of C++ Classes Both a class's data members and function members can be declared static.