C++oop and Objects---the first feature: Data abstraction

Source: Internet
Author: User

struct in C + +:(the entire struct cannot be accessed, such as: The struct cannot be output at once.) )

1): In C, structs can only contain members and cannot have functions, but C + + can. Furthermore, the function can directly access the data members inside.

2): Access mode

A): If a variable of struct type is used . Form. such as: Person.age_.

b): If the structure type of the pointer , then the form is used. such as It->age_.

Class:

1): Class Access property: ( default is Private)

Public: The element can be accessed from any location;

Private: The member is private and can only be accessed within the class.

Our general approach is to set the member variable of the class to private and to set the member function of the class to public.

This way we can only set or get the value of its member variable through a function.

The sample code is as follows:

#include <iostream> #include <string>using namespace std;class person {private:int _id;string _name;int _age ;p ublic:int get_id () const//Protect This object {  return _id;} void set_id (int id) {_id = ID;} String get_name () const {return _name;} void Set_name (const string &name) {_name = name;} int Get_age () const {return _age;}                
/*person p1;p1._age = 99;p1._id = 00123;p1._name = "Jack";p 1.print (cout); Person *P2 = new Person;p2->_age = 34;p2->_id = 12345;p2->_name = "Hello";p 2->print (cout); */
Modify the following:
Person p;
P.set_age (23);
P.SET_ID (1234);
P.set_name ("Zhangsan");
P.print (cout); }

2): member function:

Four elements: function return type, function name, formal parameter list, function body.

Attention:

A): The member function contains additional formal parameters;

The call member function is, in effect, invoked using an object, such as P1.  Print (cout), in this call, the object cout is passed, but how does print know which object's properties are printed? Therefore, the P1 is actually passed as a parameter to print.

b): The introduction of thispointer (to resolve values or references that return itself):

Each member function has an implied formal parameter that is the this pointer . This parameter points to the address of the calling object.

For example, in P1. Print (cout), the this pointer in print points to the address of the P1.

c): const member function:

such as: string Get_name () const

The meaning of the const here is that the Get_name function cannot modify this object . In fact, it is not allowed to modify the data members of the class.

Note: a normal object can call a const function, or it can call a non-const function, but a const object may only invoke a const object. (Semantic---> read-only)

4): The const member function and the normal function can form overloads.

A const member must call the const member function.

Overloaded Features: the function name (which must be the same ), the function parameter list, the name of the class, and the Const property of the member function.

There is no function overload in the C language, because the unique identifier of the C function is the function name. (resulting in redefinition);

the unique identifier of a C + + function is the function name and the parameter table composition.

1         voidPrint (Std::ostream &OS)2     {3OS <<"ID:"<< _id <<"Name:"<< _name <<"Age :"<< _age <<Endl;4     }5 6     voidPrint (Std::ostream &os)Const7      {8OS <<"ID:"<< _id <<"Name:"<< _name <<"Age :"<< _age <<Endl;9     }Ten }; One     

5): Constructor

A constructor-type special member function with the same name as the class and no return type;

A class can have more than one constructor, and a constructor must have a different number or type of formal parameter list.

Note: Each class has a default constructor, and when we define a new constructor ourselves, if we do not explicitly write out the default constructor, the program will automatically execute our newly defined constructor. This can cause a write error.

The sample code is as follows:

1 class Person2 {3      Public:4     //Where to modify: Add the default constructor to5Person ()//default constructor6: Age_ (0), Name_ ("")7         { }8         9Person (intAgeConst stringname)Ten : Age_ (age), Name_ (Name) One         { } A      -     Private: -         intAge_; the         stringname_; - } -  - intMain () + { -Person P1;//compilation error. No default constructor is explicitly provided.  +Person P2 ( at,"Zhangsan");//OK A}

Initialization of constructors: (example code above)

You must use the Initialize list scenario:

i): A class member without a default constructor;

II): const member;

III): A member of a reference type;

IIII) class members need to explicitly invoke constructors with parameters.

Consider the following class:

Class X{private:int I;int j;public:x (int val)    //If Val is 35, then i=0; j = 35;:j (val), I (j) {}};

We are supposed to initialize J with Val, initialize I with the value of J, but here the order of initialization is first I after J;

6): destructor:

~person ()

{}

Talk about the This pointer:The maximum function of this pointer is to return its own reference.

The sample code is as follows:

1#include <iostream>2#include <string>3 using namespacestd;4 5 classPerson {6 Private:7     int_id;8     string_name;9     int_age;Ten  One  Public: A  - Person (): -_ID (-1), _name ("None"), _age (-1)  the         { -     } -  -Person (intIdConst string&name,intAge ): + _id (ID), _name (name), _age (age) -         { +     } A  atPerson &set_id (intID) -         { -_id =ID; -         return* This; -     } -  inPerson &set_name (Const string&name) -         { to_name =name; +         return* This; -     } the  *Person &set_age (intAge ) $         {Panax Notoginseng_age =Age ; -         return* This; the     } +  APerson &print (Std::ostream &OS) the      { +OS <<"ID:"<< _id <<"Name:"<< _name <<"Age :"<< _age <<Endl; -         return* This; $     } $  -     ConstPerson &print (Std::ostream &os)Const -      { theOS <<"ID:"<< _id <<"Name:"<< _name <<"Age :"<< _age <<Endl; -         return* This;Wuyi     } the  - }; Wu  - intMainintargcChar**argv) { About Person p; $ P.print (cout); -P.SET_ID ( A). Print (cout). Set_age ( A). Print (cout). Set_name ("Hello"). Print ( - cout); - } A     


The above program runs as follows: 12,22,helllo and the print function it calls is the person print () type.

(If a destructor is added, it is destroyed 1 times).

If you do not add &, you are running multiple copies of the consent object. A total of 7. Call the member function several times to generate several copies.

(7 "Destroy" If you add destructors)

Static:

The static variable cannot be bound to the this pointer. Since the static variable belongs to the entire class, it does not belong to a particular object.

And this is bound to a particular object in one piece.

C++oop and Objects---the first feature: Data abstraction

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.