C + + class definition

Source: Internet
Author: User
Tags access properties class definition

C + + class definition

Defining a class is essentially a blueprint for defining a data type. This does not actually define any data, but it defines what the name of the class means, that is, it defines what the object of the class includes, and what operations can be performed on that object.

The class definition begins with the keyword class followed by the name of the class. The body of the class is enclosed in a pair of curly braces. The class definition must follow a semicolon or a list of declarations. For example, we use the keyword class to define the Box data type, as follows:

Class Box

{

Public

Double length; Length of a box

Double breadth; Breadth of a box

Double height; Height of a box

};

The keyword public determines the access properties of a class member. Within the scope of a class object, public members are accessible outside the class. You can also specify that the members of the class are private or protected, which we'll explain later.

definition C + + Object

class provides a blueprint for an object, so basically an object is created from a class. Declares an object of a class, just like a variable that declares a primitive type. The following statement declares the two objects of a class Box:

Box Box1; Declaration Box1, type Box

Box Box2; Declaration Box2, type Box

Both objects Box1 and Box2 have their own data members.

Accessing data members

The public data members of the object of the class can be accessed using the direct member access operator (.). To better understand these concepts, let's try the following example:

#include <iostream>

using namespace Std;

Class Box

{

Public

Double length; Length

Double breadth; Width

Double height; Height

};

int main ()

{

Box Box1; Declaration Box1, type Box

Box Box2; Declaration Box2, type Box

Double volume = 0.0; For storage 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;

The volume of box 1

Volume = Box1.height * Box1.length * box1.breadth;

cout << "Box1 Volume:" << volume <<endl;

The volume of Box 2

Volume = Box2.height * Box2.length * box2.breadth;

cout << "Box2 Volume:" << volume <<endl;

return 0;

}

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

Volume of Box1:210

Volume of Box2:1560

It is important to note that private members and protected members cannot be accessed directly using the direct member access operator (.). We'll learn how to access private members and protected members in a follow-up tutorial.

C + + class definition

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.