Creating Classes and Members

In C++, a class is a programming construct that you can use to define your own custom types. When you create a class, you are effectively creating a blueprint for the type. The class defines the behaviors and characteristics, or class members, which are shared by all instances of the class. You represent these behaviors and characteristics by defining methods and fields within your class.

Suppose you create a class to represent a rectangle shape in your program.  You use the class keyword to declare a class, as shown in the following example:

//Declaring a Class
class Rectangle
{
public:
    int _width;
    int _height;

};

Here we have declared a class called Rectangle and given it two public member variables called _width and _height, that will be used to represent the width and height of our rectangle.   Note that they are accessible directly because they are public, as a result of the public: modifier.

Using a Class

Now that we have a class created to represent a rectangle, we can use that in our code to create instances of a rectangle in our program.  When we create a new rectangle from this class, it is known as a rectangle object and will be given a unique name.  That way ,we can refer to it in our program directly and distinguish it from other rectangle instances that we might create, should our program require more than one.

void main()
{
     Rectangle outer;
     Rectangle inner;    

     outer._width = 10;
     outer._height = 10;

     inner._width = 5;
     inner._height = 5;
}

In this sample code, we have created two rectangle objects called outer and inner.   Then, using what is known as "dot notation" or the dot operator, we provide values for the width and height of each rectangle.  The outer rectangle is 10 x 10 and the inner rectangle is 5x5.

This contents comes from edx : Introduction to C++

저작자 표시
신고

'Tips > C++ Eng' 카테고리의 다른 글

Constructor[생성자]  (0) 2015.11.28
Introducing Classes  (0) 2015.11.28
Storage classes and Scope  (0) 2015.11.23
Introducing the for Loop  (0) 2015.11.04
The Conditional (Ternary : 3중 조건문)  (0) 2015.11.04
switch Statments  (0) 2015.11.04

WRITTEN BY
Profile
비타오백
Enjoy your stage!

받은 트랙백이 없고 , 댓글이 없습니다.
secret