zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[c++] IKM

C++
2023-09-27 14:23:24 时间

IKM考点


More: 学习笔记之IKM C++ 11

 

goto

不能在不同函数中使用goto。

 

NULL的本质

#include<stddef.h>
#include<stdio.h>
/* Define NULL pointer value */
#ifndef NULL
#ifdef __cplusplus
#define NULL    0
#else  /* __cplusplus */
#define NULL    ((void *)0)
#endif  /* __cplusplus */
#endif  /* NULL */

 

sizeof

通常策略,求数组元素的个数。

int iList[] = {1,2,3,4,5,6};
printf("%d\n", sizeof(iList)/sizeof(*iList));

sizeof(int*)占用了8字节?

一般来说,在64位系统下指针长度为8,32位系统下指针长度位4。

 

类拷贝

类的copy and deep copy。

Goto: [c++] Copy Control 

   #include<iostream>
   
   using namespace std;
   
   class Person {
   public:
       string *namePtr;
       int a = 1;
       int b = 2;
   
       Person() {namePtr = new string;}
       
       void setName(char *s) {*namePtr = s;} 
   };
   
   int main(void)
   {
       Person president;   
       Person chairman;   
   
 --    president.setName("Tom");
       cout << *president.namePtr << endl;
   
       chairman = president;
 --    cout << *chairman.namePtr << endl;
       cout << chairman.a << endl;
       cout << chairman.b << endl;
   
       return 0;
   }
View Code

 

调用父类 已被覆盖的方法 

B b;
b.A::foo()

 

namespace

C语言就没有命名空间这个概念,为了防止重名就只能使用各种naming convention,比如各种前缀。
你可以把C++的name space看作是一种语言层面支持的前缀,但是又有层级关系。
比如C中为了防止重名,你可以命名com_mycompany_mydepartment_sort
而C++中对com::mycompany::mydepartment::sort的使用,如果在同一个命名空间下,可以直接使用sort。
Goto: [c++] namespace

 

# 转换为字符串 

## 是粘贴的意思

#define MyMACRO(jkl, xyz) {#xyz ## #jkl} 

  

类中类

类中类是个新的定义,既然其他成员要用,就把声明放在最前面。也必须是public,不能是protected。

  class Bus 
   {
       public:
   
           // class Driver
           // {
           //     public:
           //         string name;
           // };
   
 >>        static Driver* createDriver()
           {
 >>            return new Driver;
           }
   
       private:
 --        int seats;
   
       protected:
           class Driver
           {
               public:
                   string name;
           };
   };
   
   
   int main(void)
   {
 >>    Bus::Driver *driver = Bus::createDriver();
       driver->name = "haojie";
       return 0;
   }
View Code

 

unamed 结构体

   int main(void)
   {
       struct {
           int yy, mm, dd;
 --    } data;
       
       data.yy = 1;
       return 0;
   }
依然可以用

 

常函数

可以使用数据成员,不能进行修改。

   #include<stdio.h>
   #include<string.h>
   #include<iostream>
   
   using namespace std;
   
   class Cart {
   
 --    bool _valid;
 --    int _fruit;
   
       static int _apples;
       static int _oranges;
   
   public:
 --    Cart(): _fruit(0), _valid(false) {
   
       }   
       
       void addApples(int a) {_apples += a; _valid = false;}
       void addOranges(int a) {_apples += a; _valid = false;}
   
       int getItemCount() const {
           if (!_valid) {
 >>            _fruit = _apples + _oranges;
 >>            _valid = true;
           }
           return _fruit;
       }   
   };
   
   
   int Cart::_apples = 0;
   int Cart::_oranges = 0;
   
   int main(void)
   {
       Cart const checkOutPerson;
       Cart shopper;
       
       shopper.addApples(2);
       shopper.addOranges(7);
   
 --    int total = checkOutPerson.getItemCount();
   
       return 0;
   }
View Code

 

一元运算符 

减号是二元,负号是一元。

  1. unary minus(-)
  2. increment(++)
  3. decrement(- -)
  4. NOT(!)
  5. Addressof operator(&)
  6. sizeof()

 

虚析构函数

保证了子类的析构函数被调用。

 #include<iostream>
 using namespace std;
 
 class Base{
 public:
     Base(){cout<<"创建Base基类。"<<endl;}
     virtual ~Base() {cout<<"删除Base基类。"<<endl;} /*虚析构函数*/
 };
 
 class Child : public Base{
 public:
     Child() {cout<<"创建Child派生类。"<<endl;}
     ~Child() {cout<<"删除Child派生类。"<<endl;} 
 };
 
 int main()
 {
     cout<<"*********虚析构函数调用示例***********"<<endl;
     Base *C1 = new Child;
     delete C1; 
 }

 

这里使用了虚的,如果是普通的,下面的 "删除Child派生类" 将消失。

$ ./a.out 
*********虚析构函数调用示例***********
创建Base基类。
创建Child派生类。
删除Child派生类。
删除Base基类。

 

 

结构体初始化

Q72. Which of the following changes must be made to the C++ code below so that the widget class definition is const-correct for its usage in main() ?

    • A. Change Widget(Widget& w); to Widget(const Widget& w);
    • B. Change Gizmo (int p, int r) ; to Gizmo (const int p, const int r);
    • C. Change Widget& operator=(Widget& rhs); to Widget& operator=(const Widget& rhs);
    • D. Change Widget (Gizmo& g) ; to Widget (const Gizmo& g);
    • E. Use const_cast during assignment otherWidget = basicWidget;
#include <iostream>

struct Gizmo {
    int pressure, rpm;
    Gizmo(int p, int r) : pressure(p), rpm(r) {}
//    Gizmo(const int p, const int r) : pressure(p), rpm(r) {}
};

struct Widget {
    int temp, speed;
    Widget() : temp (68), speed(100) {}
    
//    Widget(Widget& w);
    Widget(const Widget& w);
    
    Widget& operator=(Widget& rhs);
//    Widget& operator=(const Widget& rhs);

//    Widget(Gizmo& g);
    Widget(const Gizmo& g);
};

int main(int argc, char **argv)
{
    const Widget prototype;

    Widget basicWidget = prototype; // No matching constructor for initialization of 'Widget'

    Widget otherWidget;
    otherWidget = basicWidget;

    const Gizmo gadget(10, 100);

    Widget thirdWidget(gadget); // No matching constructor for initialization of 'Widget'

    return 1;
}

 

End.