zoukankan      html  css  js  c++  java
  • 网易云课堂_C++程序设计入门(上)_第4单元:物以类聚 – 对象和类

    第1节:类的概念

    第2节:创建对象并访问对象成员

    第3节:关于创建对象的更多细节

    第4节:将声明与实现分离

    第5节:对象指针与动态对象

    第6节:C++字符串类

    第7节:数据域封装

    第1节:类的概念

    Features of OO ( 面向对象的特征)

    Abstraction ( 抽象)

    Polymorphism ( 多态)

    Inheritance ( 继承)

    Encapsulation ( 封装)

    A PIE

    第2节:创建对象并访问对象成员

    第3节:关于创建对象的更多细节

    Naming Objects and Classes ( 为对象和类命名)

    When you declare a custom class, capitalize the first letter of each word in a class name; ( 声明一个自定义的类时,类名中的单词要首字母大写) for example, the class names Circle, Rectangle, and Desk.

    Class is a Type ( 类是一种数据类型)

    3. Names representing types must be in mixed case starting with upper case.

    3. 代表类型的名字必须首字母大写并且其它字母大小写混合

    例如:Line, SavingsAccount

    第4节:将声明与实现分离

    Inline Declaration & Inline Function ( 内联声明与内联函数)

    When a function is implemented inside a class declaration, it automatically becomes an inline function. ( 当函数在类声明中实现,它自动成为内联函数)

    class A
    {
    public:
    	A()
    	{
    		// do something;
    	}
    	double f1()
    	{
    		// return a number
    	}
    	double f2();
    };
    inline double A::f2()
    {
    	//do something
    }
    

    第5节:对象指针与动态对象

    Creating Dynamic Objects on Heap ( 在堆中创建对象)

    Object declared in a function is created in the stack.(在函数中声明的对象都 在栈上创建); When the function returns, the object is destroyed (函数返回, 则对象被销毁).

    To retain the object, you may create it dynamically on the heap using the new operator. ( 为保留对象,你可以用new运算符在堆上创建它)

    ClassName *pObject = new ClassName(); //用无参构造函数创建对象
    Circle *pCircle1 = new Circle(); //用无参构造函数创建对象
    
    Circle *pCircle2 = new Circle(5.9); //用有参构造函数创建对象
    ClassName *pObject = new ClassName(arguments); //用有参构造函数创建对象
    
    //程序结束时,动态对象会被销毁,或者
    delete pObject; //用delete显式销毁
    

    第6节:C++字符串类

    The C++ string Class

    C++ 使用 string 类处理 字符串

    string类中的函数

    1. 构造

    2. 追加

    3. 赋值

    4. 位置与清除

    5. 长度与容量

    6. 比较

    7. 子串

    8. 搜索

    9. 运算符

    Appending a String ( 追加字符串)

    You can use several overloaded functions to add new contents to a string. (一系列的重载函数可以将新内容附加到一个字符串中)

    string s1("Welcome");
    s1.append(" to C++"); // appends " to C++" to s1
    cout << s1 << endl; // s1 now becomes Welcome to C++
    
    string s2("Welcome");
    s2.append(" to C and C++", 3, 2); // appends " C" to s2
    cout << s2 << endl; // s2 now becomes Welcome C
    
    string s3("Welcome");
    s3.append(" to C and C++", 5); // appends " to C" to s3
    cout << s3 << endl; // s3 now becomes Welcome to C
    
    string s4("Welcome");
    s4.append(4, 'G'); // appends "GGGG" to s4
    cout << s4 << endl; // s4 now becomes WelcomeGGGG
    

    Functions at, clear, erase, and empty

    at(index): 返回当前字符串中index位置的字符

    clear(): 清空字符串

    erase(index, n): 删除字符串从index开始的n个字符

    empty(): 检测字符串是否为空

    string s1("Welcome");
    cout << s1.at(3) << endl; // s1.at(3) returns c
    cout << s1.erase(2, 3) << endl; // s1 is now Weme
    s1.clear(); // s1 is now empty
    cout << s1.empty() << endl; // s1.empty returns 1 (means true)
    

    Comparing Strings ( 比较字符串)

    compare() 函数用于比较两个字符串。它与C语言中的 strcmp() 函数很像。

    string s1("Welcome");
    string s2("Welcomg");
    
    cout << s1.compare(s2) << endl; // returns -2 e-g==-2
    cout << s2.compare(s1) << endl; // returns 2 g-e==2
    
    cout << s1.compare("Welcome") << endl; // returns 0
    

    Obtaining Substrings ( 获取子串)

    at() 函数用于获取一个单独的字符;而substr() 函数则可以获取一个子串

    string s1("Welcome");
    cout << s1.substr(0, 1) << endl; // returns W ; 从0号位置开始的1个字符
    cout << s1.substr(3) << endl; // returns come ; 从3号位置直到末尾的子串
    cout << s1.substr(3, 3) << endl; // returns com ;从3号位置开始的3个字符
    

    Searching in a String ( 搜索字符串)

    find() 函数可以在一个字符串中搜索一个子串或者一个字符

    string s1("Welcome to HTML");
    cout << s1.find("co") << endl; // returns 3 ; 返回子串出现的第一个位置
    cout << s1.find("co", 6) << endl; // returns -1 从6号位置开始查找子串出现的第一个位置
    cout << s1.find('o') << endl; // returns 4 返回字符出现的第一个位置
    cout << s1.find('o', 6) << endl; // returns 9 从6号位置开始查找字符出现的第一个位置
    

    Inserting and Replacing Strings ( 插入和替换字符串)

    insert() : 将某个字符/字符串插入到当前字符串的某个位置

    replace() 将本字串从某个位置开始的一些字符替换为其它内容

    string s1("Welcome to HTML");
    s1.insert(11, "C++ and ");
    cout << s1 << endl; // s1 becomes Welcome to C++ and HTML
    
    string s2("AA");
    s2.insert(1, 4, 'B'); // 在1号位置处连续插入4个相同字符
    cout << s2 << endl; // s2 becomes to ABBBBA
    
    string s3("Welcome to HTML");
    s3.replace(11, 4, "C++"); // 从11号位置开始向后的4个字符替换掉。注意''
    cout << s3 << endl; // returns Welcome to C++
    

    String Operators ( 字符串运算符)

    string s1 = "ABC"; // The = operator string
    s2 = s1; // The = operator
    for (int i = s2.size() - 1; i >= 0; i--)
    cout << s2[i]; // The [] operator
    
    string s3 = s1 + "DEFG"; // The + operator
    cout << s3 << endl; // s3 becomes ABCDEFG
    
    s1 += "ABC";
    cout << s1 << endl; // s1 becomes ABCABC
    
    s1 = "ABC";
    s2 = "ABE";
    cout << (s1 == s2) << endl; // Displays 0
    cout << (s1 != s2) << endl; // Displays 1
    cout << (s1 > s2) << endl; // Displays 0
    cout << (s1 >= s2) << endl; // Displays 0
    cout << (s1 < s2) << endl; // Displays 1
    cout << (s1 <= s2) << endl; // Displays 1
    

    Operator Description

    [ ] 用数组下标运算符访问字符串中的字符

    = 将一个字符串的内容复制到另一个字符串

    + 连接两个字符串得到一个新串

    += 将一个字符串追加到另一个字符串末尾

    << 将一个字符串插入一个流

    >> 从一个流提取一个字符串,分界符为空格 或者空结束符

    ==, !=, <, <=, >, >= 用于字符串比较

    第7节:数据域封装

    Accessor and Mutator ( 访问器与更改器)

    To read/write private data, we need get/set function ( 为读写私有数据,需要get/set函数)

    get function is referred to as a getter (获取器,or accessor),

    set function is referred to as a setter (设置器,or mutator).

    Signature of get function (General form) (get函数的 一般原型)

    returnType getPropertyName()

    Signature of get function (Bool type) (布尔型get函数 的原型)

    bool isPropertyName()

    Signature of set function (set函数的原型)

    void setPropertyName(dataType propertyValue)

    26. 布尔变量/函数的命名应使用前缀“is”

    There are a few alternatives to the is prefix that fit better in some situations. These are the has, can and should prefixes:

    "is" 前缀有时会有更好的替换,包括has, can 和should

    例如:bool hasLicense(); bool canEvaluate(); bool shouldSort();

    第8节:变量作用域与this指针

    The Scope of Variables – Review ( 变量作用域-回顾)

    C语言的“函数”章节,介绍了3种变量作用域

    Global variables (全局变量)

    are declared outside all functions and are accessible to all functions in its scope. (在所 有函数外面声明并在其作用域内可被所有函数访问)

    The scope starts from its declaration and continues to the end of the program. (作用 域起于声明,止于程序结束)

    Local variables (局部变量)

    are defined inside functions. (在函数内定义)

    The scope starts from its declaration and continues to the end of the block that contains the variable. ( 作用域起于声明,止于包含该变量的块尾)

    Static local variables (静态局部变量)

    permanently stored in the program.

    can be used in the next call of the function

    Simple way to avoid name hidden ( 避免重名屏蔽的简单方法)

    class Circle
    {
    public:
    	Circle();
    	Circle(double radius)
    	{
    		//this->radius = radius;
    		radius_ = radius;
    	}
    private:
    	double radius_;
    public:
    	void setRadius(double);
    	//……
    };
    

    11. Private class variables should have underscore suffix.

    11. 私有类成员变量名应有下划线后缀

    例:

    class SomeClass
    {
    private:
    	int length_;
    }
    

    代码中容易区分类成员变量及函数局部变量

    也有些规范中使用下划线前缀。但使用后缀让名字可读性更好

    第9节:对象作为函数参数

    Array of Objects ( 对象数组)

    声明方式1

    Circle circleArray[10];

    Question:

    circleArray[1].getRadius() 的值是 多少?

    声明方式2

    Circle circleArray[3] =
    {
    	Circle(3),
    	Circle(4),
    	Circle(5)
    };
    

    第10节:对象数组

    第11节:类抽象与封装

    第12节:构造函数初始化列表

    The Role of Default Constructor ( 默认构造函数的角色)

    Note

    若类的数据域是一个对象类型(且它没有无参构造 函数),则该对象初始化必须放在初始化列表中

  • 相关阅读:
    webrtc连接方法——TURN服务器和STUN服务器作用简介
    IM音视频即时通讯系统EasyRTC如何利用webrtc技术进行优化和发展?
    TSINGSEE青犀视频平台可以实现音视频混流吗?
    深入浅出Java 重定向和请求转发的区别
    特征工程-特征提取
    cart剪枝
    决策树算法简介
    逻辑回归介绍
    sklearn模型的保存和加载
    celery 定时任务时间篇
  • 原文地址:https://www.cnblogs.com/denggelin/p/5874364.html
Copyright © 2011-2022 走看看