Introduction to the _c++ programming of NetEase Cloud Classroom (Part I) _ Unit 5th: Water Frost Days Freedom – more about objects and classes

Source: Internet
Author: User

Section 1th: Immutable objects, immutable classes; avoid multiple declarations

4. Variable names must is in the mixed case starting with lower case.

4. Variable names must be mixed case and start with lowercase letters

Example: Line, SavingsAccount

How do you make a class immutable? (Let class become immutable Class)

Mark All data field private (all domains are set to "private" property)

It's All mine!

No mutator functions (no change function)

No one's moving!

No accessor that would return a reference/pointer to a mutable data field object (nor is there a reference or pointer accessor that can return a mutable data Field object)

Sneak can not touch!

For multithreaded programming immutable Object:thread-safe

Preventing multiple Declarations (avoid multiple declarations)

1. Put "#pragma once" in the first line of the. h file (using "Pragma")

relies on the compiler

The old compiler does not support

2. Use #ifndef preprocessing instructions. h file (using "macro")

#ifndef Filename_h

#define Filename_h

The contents of the header file

#endif Filename_h

Section 2nd: Instance members vs. static members

Rules for static member function (rule with static member function)

Rules1:how to invoke static member function: (call static member function)

Rules2:static member functions Access other Members: (Static member function accesses other member)

be visitedKeynote functionStaticnon-staticStaticby class name/object nameonly by object namenon-staticonly by object nameuse class name (for readablity) (Access static variables/functions using the class name)Use classname::functionname (arguments) to invoke a static function and classname::staticvariable.
#include <iostream>class a{public:a (int A = 0)//constructor {x = A;} static void F1 ();//statically member function static void F2 (a a);//static member function private:int x;static int y;//static data member};void A::F1 () {Std::cout < < a::y << Std::endl;} void A::f2 (A a) {std::cout << a::y << std::endl;std::cout << a.x << Std::endl;} int a::y = 25;//Initialize static data member void Main () {a::f1 (); A MA (3); A::F2 (MA); ma.a::f1 (); System ("Pause");}

Instance or Static? (instance or static)

When is the use of STATIC in class? (when static members are used in a class)

A variable or function that is not dependent on a specific instance of the class should be a static variable or function. (Variables and functions do not depend on instances of the class)

For example

Every circle has its own radius. Radius is dependent on a specific circle. Therefore, RADIUS is an instance variable of the Circle class. Since The Getarea function is dependent on a specific circle, it's an instance function.

Since numberofobjects is not dependent on any specific instance, it should be declared static.

Section 3rd: Destructors and friends

Section 4th: Copy Constructors

Section 5th: Example Analysis

Section 6th: Vector class

Section 7th: More Coding specifications

Introduction to the _c++ programming of NetEase Cloud Classroom (Part I) _ Unit 5th: Water Frost Days Freedom – more about objects and classes

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.