C + + Learning path "Object oriented"

Source: Internet
Author: User
Tags access properties class access modifiers class definition function definition modifiers

C + + has added object-oriented programming on the basis of the language of C., C + + supports object-oriented programming. Classes are the core features of C + +.

Examples of classes:

Class box{public
:
    int length;
    int width;
    int height;
};
int main (int argc, const char * argv[]) {
    Box box1;
    }

It is to be noted that private members and protected members cannot be accessed directly using the direct member access operator (.). class member functions

A member function of a class is one that writes definitions and prototypes within the class definition, just like any other variable in the class definition. A class member function is a member of a class that can manipulate any object of a class and access all members of the object.

Writing 1: (defining member functions within the class)

Class box{public
:
    int length;
    int width;
    int height;
    int Getvolume () {return
        length * width * height;
    }
};
int main (int argc, const char * argv[]) {
    Box box1;
    Box1.length = ten;
    Box1.width = ten;
    Box1.height = ten;
    cout << box1.getvolume () << Endl;
}

Writing 2: (only within class to declare a member function, used outside the class:: symbol to define member functions)

Class box{public
:
    int length;
    int width;
    int height;    member function declaration
    int getvolume ();
};
member function definition
int box::getvolume () {return
    length * width * height;
}

int main (int argc, const char * argv[]) {
    Box box1;
    Box1.length = ten;
    Box1.width = ten;
    Box1.height = ten;
    cout << box1.getvolume () << Endl;
}

The complete code for defining class box is as follows:

Class box{public
:
    int length;
    int width;
    int height;
    int Getvolume ();
    void SetLength (int l);
    void setheight (int h);
    void SetWidth (int w);
void Box::setwidth (int w) {
    width = w;
}
void box::setheight (int h) {
    height = h;
}
void box::setlength (int l) {
    length = l;
}
int Box::getvolume () {return
    length * width * height;
}

int main (int argc, const char * argv[]) {
    box box;
    Box.setlength (ten);
    Box.setheight (a);
    Box.setwidth ();
    cout << box.getvolume () << Endl;
}

Ps:
:: Called scope specifier, indicating which class A function belongs to, or which class the data belongs to.
:: You can represent global data or global functions (that is, non-member functions) without the class name.

int month;//global variable
int day;
int year;
void Set (int m,int d,int y)
{
    :: year=y;//Assign values to global variables, which can be omitted here
    ::d ay=d;
    :: month=m;
}

Class tdate
{public
    :
        void Set (int m,int d,int y)//member function
        {
            :: Set (M,d,y);//non-member function
        }
    Private:
        int month;
        int day;
        int year;
}
class access Modifiers Data Encapsulationis an important feature of object-oriented programming, and it prevents functions from directly accessing internal members of class types。 The access restrictions for class members are passed within the class body for each areaMark Public, private, protected to specify. The keywords public, private, and protected are called access modifiers. A class can have more than one public, protected, or private tag area. Each markup range is valid before the next tag area starts or before the class body ends the closing parenthesis. The default access modifier for members and classes is private.
Class Base {

   public:

  //publicly member

   protected:

  //Protected member

   private:

  //Private member

};
Public members are accessible outside of the class in the program. You can set and get the values of public variables without using any member functions. That is, you can manipulate the member variables in class directly outside of class. A private member variable or function is not accessible or even viewable outside of the class. Only classes and friend functions can access private members. A private variable can only be manipulated by a member function in class. Protecting a member variable or function is very similar to a private member, but one point is that the protection member is accessible in a derived class (that is, a subclass).

The difference between public and private is as follows:

Class student{
private:
    int age;
Public:
    string name;
    void Setage (int a);
    int getage ();
};
void Student::setage (int a) {age
    = A;
}
int Student::getage () {return age
    ;
}

int main (int argc, const char * argv[]) {
    Student stu;
    Stu.name = "Bob";
    Stu.setage (in);
    cout << stu.name << Endl;
    cout << stu.getage () << Endl; 
}
the characteristics of the inheritance

There are public, protected, private three ways of inheriting, which change the access properties of the base class members accordingly.
1.public Inheritance: base class public members, protected members, access properties of private members in derived classes, respectively: public, protected, private
2.protected Inheritance: base class public member, protected member, private member's Access property in derived class, respectively: protected, protected, private
3.private Inheritance: base class public member, protected member, private member's access attribute in derived class, respectively: Private, private, private
But either way, none of the above two points have changed:
1.private members can only be accessed by members of this class (within the Class) and friends, and cannot be accessed by derived classes;
2.protected members can be accessed by derived classes. Constructors for classes

The constructor of a class is a special member function of a class that is executed each time a new object of the class is created.
The name of the constructor is exactly the same as the name of the class, and no type is returned and void is not returned. Constructors can be used to set initial values for some member variables.

Not only member functions can be used:: Defined outside class, constructors can also be defined outside of class.

As you can see from the code above, a constructor can initialize the value of a member variable, and another way to do this is to do this.


destructor

is to delete the created object's function.
The name of the destructor is exactly the same as the name of the class, but preceded by a tilde (~) prefix, it does not return any values or take any arguments. Destructors help you release resources before you jump out of a program, such as closing a file, freeing memory, and so on.

Simple example:

Contains constructors and destructors.

The constructor was called when the object was created, and the destructor was automatically invoked at the end of the program. Destructors exist by default.

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.