C/C + + Academy (7) A discussion of the object-oriented model

Source: Internet
Author: User

1. Basic knowledge C + +in theclassStarting from the object-oriented theory, the variable(Properties)and Functions(Method)centrally defined together to describe classes in the real world. From the computer's point of view, the program is still composed of data segments and code snippets.

#include "iostream" using namespace Std;class c1{public:int i;//4 int J;//4int k;//4protected:private:}; 12class C2{public:int i;//4int j;//4int K;//4static int m;//4public:int getk () const {return k;}//4void setk (int val) {k = val;}//4protected:private:}; 12struct s1{int i;int j;int k;}; 12struct s2{int i;int j;int k;static int m;}; 12int Main () {printf ("c1:%d \ n", sizeof (C1));p rintf ("c2:%d \ n", sizeof (C2));p rintf ("s1:%d \ n", sizeof (S1));p rintf (" s2:%d \ n ", sizeof (S2)); System (" Pause ");}


2, four-zone interpretation with memoryC + +Properties and Methods2.1 C + +The member variables and member functions in the class object are stored separately

Member variables:

Normal member variables: stored in an object, with the same memory layout and byte alignment as the struct variable

Static member variables: stored in the global data area

member functions: stored in code snippets.

2.2 C + +compiler's internal processing of ordinary member functions


3, Summary:

The member variables and member functions in 3.1 C + + class objects are stored separately. Memory four-zone model in C language still works!

3.2 C + + The normal member function of a class implicitly contains a pointer to the current object. This pointer.

3.3 Static member functions, member variables belong to class

The difference between a static member function and a normal member function

Static member functions do not contain pointers to specific objects

The normal member function contains a pointer to a specific object


C/C + + Academy (7) A discussion of the object-oriented model

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.