戳人痛处

导航

[MFC-学习笔记]--- C++学习done[01]

]1.using namespace std;

#using namespace std - 搜狗百科 (sogou.com)

这样命名空间std内定义的所有标识符都有效(曝光)。就好像它们被声明为全局变量一样。那么以上语句可以如下写: cout << hex << 3.4 << endl;

#【C++】std::是什么? - mhq_martin - 博客园 (cnblogs.com)

 std::是个名称空间标示符,C++标准库中的函数或者对象都是在命名空间std中定义的,所以我们要使用标准函数库中的函数或对象都要使用std来限定。

#sololearn

namespace is a declarative region that provides a scope to the identifiers (names of elements) inside it.
In our code, the line using namespace std; tells the compiler to use the std (standard) namespace:

]2.auto x1 = 5, x2 = 5.0, x3='r';

#C++ auto 关键字的使用 - 自由真实个性 - 博客园 (cnblogs.com)

auto的原理就是根据后面的值,来自己推测前面的类型是什么。

]3.Constructors 构造函数

#C++构造函数详解 (biancheng.net)

When creating an object, you now need to pass the constructor's parameter, as you would when calling a function:

]4.Menber Initializers

#C++ 成员变量的初始化顺序问题详解_C 语言_脚本之家 (jb51.net)

Recall that constants are variables that cannot be changed, and that all const variables must be initialized at time of creation.

C++ provides a handy syntax for initializing members of the class called the member initializer list (also called a constructor initializer).

Consider the following class:

class MyClass {
  public:
   MyClass(int a, int b) {
    regVar = a;
    constVar = b;
   }
  private:
    int regVar;
    const int constVar;
};

This class has two member variables, regVar and constVar. It also has a constructor that takes two parameters, which are used to initialize the member variables.
Running this code returns an error, because one of its member variables is a constant, which cannot be assigned a value after declaration.

In cases like this one, a member initialization list can be used to assign values to the member variables.

 1 class MyClass {
 2  public:
 3   MyClass(int a, int b)
 4   : regVar(a), constVar(b)
 5   {
 6   }
 7  private:
 8   int regVar;
 9   const int constVar;
10 };

Note that in the syntax, the initialization list follows the constructor parameters. The list begins with a colon (:), and then lists each variable to be initialized, along with the value for that variable, with a comma to separate them.
Use the syntax variable(value) to assign values.

The initialization list eliminates the need to place explicit assignments in the constructor body. Also, the initialization list does not end with a semicolon.

Let's write the previous example using separate header and source files.

MyClass.h
1 class MyClass {
2   public:
3    MyClass(int a, int b);
4   private:
5    int regVar;
6    const int constVar;
7 };
MyClass.cpp
1 MyClass::MyClass(int a, int b)
2 : regVar(a), constVar(b)
3 {
4   cout << regVar << endl;
5   cout << constVar << endl;
6 }

We have added cout statements in the constructor to print the values of the member variables.
Our next step is to create an object of our class in main, and use the constructor to assign values.

The constructor is used to create the object, assigning two parameters to the member variables via the member initialization list.

The member initialization list may be used for regular variables, and must be used for constant variables.

Even in cases in which member variables are not constant, it makes good sense to use the member initializer syntax.

]5.友元函数

#C++ 友元函数 - balingybj - 博客园 (cnblogs.com)

具体来说:为了使其他类的成员函数直接访问该类的私有变量

Normally, private members of a class cannot be accessed from outside of that class.
However, declaring a non-member function as a friend of a class allows it to access the class' private members. This is accomplished by including a declaration of this external function within the class, and preceding it with the keyword friend.

In the example below, someFunc(), which is not a member function of the class, is a friend of MyClass and can access its private members.

 1 class MyClass {
 2  public:
 3   MyClass() {
 4    regVar = 0;
 5   }
 6  private:
 7   int regVar;
 8     
 9   friend void someFunc(MyClass &obj);
10 };
Note that when passing an object to the function, we need to pass it by reference, using the & operator.

The function someFunc() is defined as a regular function outside the class. It takes an object of type MyClass as its parameter, and is able to access the private data members of that object.

 1 class MyClass {
 2  public:
 3   MyClass() {
 4    regVar = 0;
 5   }
 6  private:
 7   int regVar;
 8     
 9  friend void someFunc(MyClass &obj);
10 };
11 
12 void someFunc(MyClass &obj) {
13   obj.regVar = 42;
14   cout << obj.regVar;
15 }

The someFunc() function changes the private member of the object and prints its value.

To make its members accessible, the class has to declare the function as a friend in its definition. You cannot "make" a function a friend to a class without the class "giving away" its friendship to that function.

someFunc() had the ability to modify the private member of the object and print its value.

Typical use cases of friend functions are operations that are conducted between two different classes accessing private members of both.

You can declare a function friend across any number of classes.
Similar to friend functions, you can define a friend class, which has access to the private members of another class.

]6.运算符重载

#C++ 重载运算符和重载函数 | 菜鸟教程 (runoob.com)

您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。

]7.public/private/protected

#public/private/protected的具体区别_百度知道 (baidu.com)

protected:protected对于子女、朋友来说,就是public的,可以自由使用,没有任何限制,而对于其他的外部class,protected就变成private。

#C++中public、protected、private的区别_SCOTT 技术博客-CSDN博客_private

protected: 可以被该类中的函数、子类的函数、以及其友元函数访问,但不能被该类的对象访问

]8.虚函数

#C++虚函数详解_C语言中文网 (biancheng.net)

虚函数常与多态配合使用;

Base *p = new Derived();

通过基类的指针只能访问从基类继承过去的成员,不能访问派生类新增的成员。

]]_Pure Virtual Function

In some situations you'd want to include a virtual function in a base class so that it may be redefined in a derived class to suit the objects of that class, but that there is no meaningful definition you could give for the function in the base class.
The virtual member functions without definition are known as pure virtual functions. They basically specify that the derived classes define that function on their own.
The syntax is to replace their definition by =0 (an equal sign and a zero):

1 class Enemy {
2  public:
3   virtual void attack() = 0;
4 }; 
The = 0 tells the compiler that the function has no body.

pure virtual function basically defines, that the derived classes will have that function defined on their own.
Every derived class inheriting from a class with a pure virtual function must override that function.

If the pure virtual function is not overridden in the derived class, the code fails to compile and results in an error when you try to instantiate an object of the derived class.

 You cannot create objects of the base class with a pure virtual function.
Running the following code will return an error:

]9.函数模板

#C++函数模板(模板函数)详解 (biancheng.net)

template <class T, class U>
T smaller(T a, U b) {
  return (a < b ? a : b);
}

 替代

int smaller(int a,int b);
float smaller(float a,float b);
etc...

]10.try/catch/throw

#c++中try catch的用法 - 超酷小子 - 博客园 (cnblogs.com)

try block identifies a block of code that will activate specific exceptions. It's followed by one or more catch blocks. The catch keyword represents a block of code that executes when a particular exception is thrown.
Code that could generate an exception is surrounded with the try/catch block.
You can specify what type of exception you want to catch by the exception declaration that appears in parentheses following the keyword catch.

#include <iostream>
using namespace std;

int main()
{
    try {
        int motherAge = 29;
        int sonAge = 36;
        if (sonAge > motherAge) {
            throw 99;
        }
    }
    catch (int x) {
        cout<<"Wrong age values - Error "<<x;
    }

    return 0;
}
#Wrong age values - Error 99

posted on 2021-07-15 16:58  戳人痛处  阅读(172)  评论(0编辑  收藏  举报