总结C++基础知识(part1)

1.友元函数,友元类

对于一个没有定义public访问权限的类,能够让其他的类操作它的私有成员往往是有用的。例如你写了一段binary tree的代码,Node是节点类,如果能够让连接多个节点的函数不需要调用public方法就能够访问到Node的私有成员的话,一定是很方便的。

Friend Classes(友元类)

C++中的friend关键字其实做这样的事情:在一个类中指明其他的类(或者)函数能够直接访问该类中的private和protected成员。

你可以这样来指明:

friend class aClass;
注意:friend在类中的声明可以再public、protected和private的如何一个控制域中,而不影响其效果。例如,如果你在protected域中有这样的声明,那么aClass类同样可以访问该类的private成员。

这有一个例子:

[html]  view plain copy print ?
  1. class Node   
  2. {  
  3.     private:   
  4.        int data;  
  5.        int key;  
  6.        // ...  
  7.   
  8.     friend class BinaryTree; // class BinaryTree can now access data directly  
  9. };  
这样BinaryTree就可以直接访问Node中的private的成员了,就像下面这样:


Friend Functions(友元函数)

友元函数和友元类的作用是一样的,它允许一个函数不需要通过其public接口就能够访问到类中的private和protected成员变量。

你可以这样去声明:

friend return_type class_name::function(args);
这里有一个例子:

class Node 
{
    private: 
       int data;
       int key;
       // ...

    friend int BinaryTree::find(); // Only BinaryTree's find function has access
};
这样find方法就可以直接访问Node中的私有成员变量了,而BinaryTree中的其他的方法去不能够直接的访问Node的成员变量。(这就是友元类和友元函数的区别)

2. Define inheritance?

The mechanism of deriving a new class (derived) from an old class (base class) is called inheritance. It allows the extension and reuse of existing code without having to rewrite the code from scratch. Inheritance is the process by which objects of one class acquire properties of objects of another class.

3. Define Constructors?

A constructor is a member function with the same name as its class. The constructor is invoked whenever an object of its associated class is created. It is called constructor because it constructs the values of data members of the class.

4. What is the difference between C & C++?

C++ is an object oriented programing but c is a procedure oriented programing. C is super set of C++. C can’t support inheritance, function overloading, method overloading etc. but C++ can do this. In c-program the main function could not return a value but in the C++ the main function should return a value.

5. What do you mean by implicit conversion?

  • Whenever data types are mixed in an expression then C++ performs the conversion automatically.
  • Here smaller type is converted to wider type.

Example : in case of integer and float integer is converted into float type.

6. What is the difference between class and structure?

  • By default, the members of structures are public while that tor class is private.
  • Structures doesn’t provide something like data hiding which is provided by the classes.
  • Structures contains only data while class bind both data and member functions.

7. What is dynamic binding?

Dynamic binding (also known as late binding) means that the code associated with a given procedure call is not known until the time of the call at run time. It is associated with polymorphism and inheritance.

8.function overloading, operator overloading, reduce function name number

function->parameter type, number; operator->parameter type only, “operator ”keyword

same return type

9. what is class invariant?

A class invariant is a condition that defines all valid states for an object. It is a logical condition to ensure the correct working of a class. Class invariants must hold when an object is created, and they must be preserved under all operations of the class. In particular all class invariants are both preconditions and post-conditions for all operations or member functions of the class.

10. What is an iterator?

Iterators are like pointers. They are used to access the elements of containers thus providing a link between algorithms and containers. Iterators are defined for specific containers and used as arguments to algorithms.

11. What are the differences between new and malloc?

  • New initializes the allocated memory by calling the constructor. Memory allocated with new should be released with delete.
  • Malloc allocates uninitialized memory.
  • The allocated memory has to be released with free. New automatically calls the constructor while malloc (doesn’t).
  • 1.malloc与free是C++/C语言的标准库函数,new/delete是C++的运算符。它们都可用于申请动态内存和释放内存

    2.对于非内部数据类型的对象而言,光用maloc/free无法满足动态对象的要求。对象在创建的同时要自动执行构造函数,对象在消亡之前要自动执行析构函数。由malloc/free是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加于malloc/free。

    3.因此C++语言需要一个能完成动态内存分配和初始化工作的运算符new,以一个能完成清理与释放内存工作的运算符delete。注意new/delete不是库函数。 
    4.C++程序经常要调用C函数,而C程序只能用malloc/free管理动态内存。 
    5.new可以认为是malloc加构造函数的执行。new出来的指针是直接带类型信息的。而malloc返回的都是void*指针。

    new delete在实现上其实调用了malloc,free函数

    6.new建立的对象你可以把它当成一个普通的对象,用成员函数访问,不要直接访问它的地址空间;malloc分配的是一块内存区域,就用指针访问好了,而且还可以在里面移动指针.

    7.new 建立的是一个对象;alloc分配的是一块内存.

    ***************************************

    相同点:都可用于申请动态内存和释放内存

    不同点: 
    (1)操作对象有所不同。 
    malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符。对于非内部数据类的对象而言,光用maloc/free 无法满足动态对象的要求。对象在创建的同时要自动执行构造函数, 对象消亡之前要自动执行析构函数。由于malloc/free 是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加malloc/free。

    (2)在用法上也有所不同。 
    函数malloc 的原型如下: 
    void * malloc(size_t size); 
    用malloc 申请一块长度为length 的整数类型的内存,程序如下: 
    int *p = (int *) malloc(sizeof(int) * length); 
    我们应当把注意力集中在两个要素上:“类型转换”和“sizeof”。 
    malloc 返回值的类型是void *,所以在调用malloc 时要显式地进行类型转换,将void * 转换成所需要的指针类型。 
    malloc 函数本身并不识别要申请的内存是什么类型,它只关心内存的总字节数。

    函数free 的原型如下: 
    void free( void * memblock ); 
    为什么free 函数不象malloc 函数那样复杂呢?这是因为指针p 的类型以及它所指的内存的容量事先都是知道的,语句free(p)能正确地释放内存。如果p 是NULL 指针,那么free

    对p 无论操作多少次都不会出问题。如果p 不是NULL 指针,那么free 对p连续操作两次就会导致程序运行错误。

    new/delete 的使用要点 
    运算符new 使用起来要比函数malloc 简单得多,例如: 
    int *p1 = (int *)malloc(sizeof(int) * length); 
    int *p2 = new int[length]; 
    这是因为new 内置了sizeof、类型转换和类型安全检查功能。对于非内部数据类型的对象而言,new 在创建动态对象的同时完成了初始化工作。如果对象有多个构造函数,那么new 的语句也可以有多种形式。

    如果用new 创建对象数组,那么只能使用对象的无参数构造函数。例如 
    Obj *objects = new Obj[100]; // 创建100 个动态对象 
    不能写成 
    Obj *objects = new Obj[100](1);// 创建100 个动态对象的同时赋初值1 
    在用delete 释放对象数组时,留意不要丢了符号‘[]’。例如 
    delete []objects; // 正确的用法 
    delete objects; // 错误的用法 
    后者相当于delete objects[0],漏掉了另外99 个对象。

    ***************************************

    1  new自动计算需要分配的空间,而malloc需要手工计算字节数 
    2  new是类型安全的,而malloc不是,比如: 
    int* p = new float[2]; // 编译时指出错误 
    int* p = malloc(2*sizeof(float)); // 编译时无法指出错误 
    new operator 由两步构成,分别是 operator new 和 construct 
    3  operator new对应于malloc,但operator new可以重载,可以自定义内存分配策略,甚至不做内存分配,甚至分配到非内存设备上。而malloc无能为力 
    4  new将调用constructor,而malloc不能;delete将调用destructor,而free不能。 
    5  malloc/free要库文件支持,new/delete则不要。

12. What is an explicit constructor?

A conversion constructor declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. It’s purpose is reserved explicitly for construction. Explicit constructors are simply constructors that cannot take part in an implicit conversion.

explicit只对构造函数起作用,用来抑制隐式转换,如下:

[cpp]  view plain copy
  1. class A  
  2. {  
  3.     A(int a);  
  4. };  
  5.   
  6. int Function(A a);  
当调用Function(2)的时候,会隐式转换为A类型。这种情况常常不是我们所想要的结果,所以,要避免这种情形,我们就可以这样写:

[cpp] view plaincopyclass A  

  1. {  
  2.     explicit A(int a);  
  3. };  
  4.   
  5. int Function(A a);  
这样,当调用Function(2)的时候,编译器会给出错误的信息(除非Function有个以int为参数的重载形式),这就避免了在我们毫不知情的情况下出现的错误。

13. What do you mean by inline function?

An inline function is a function that is expanded inline when invoked.ie. the compiler replaces the function call with the corresponding function code. An inline function is a function that is expanded in line when it is invoked. That is the compiler replaces the function call with the corresponding function code (similar to macro).

reduce calling time

14. What is the difference between a copy constructor and an overloaded assignment operator?

A copy constructor constructs a new object by using the content of the argument object. An overloaded assignment operator assigns the contents of an existing object to another existing object of the same class.

The purpose of the copy constructor and the assignment operator are almost equivalent — both copy one object to another. However, the assignment operator copies to existing objects, and the copy constructor copies to newly created objects.

The difference between the copy constructor and the assignment operator causes a lot of confusion for new programmers, but it’s really not all that difficult. Summarizing:

  • If a new object has to be created before the copying can occur, the copy constructor is used.
  • If a new object does not have to be created before the copying can occur, the assignment operator is used.

There are three general cases where the copy constructor is called instead of the assignment operator:

  1. When instantiating one object and initializing it with values from another object (as in the example above).
  2. When passing an object by value.
  3. When an object is returned from a function by value.

In each of these cases, a new variable needs to be created before the values can be copied — hence the use of the copy constructor.

Because the copy constructor and assignment operator essentially do the same job (they are just called in different cases), the code needed to implement them is almost identical.

15.pass by value, pass by reference

Say I want to share a web page with you.

If I tell you the URL, I'm passing by reference. You can use that URL to see the same web page I can see. If that page is changed, we both see the changes. If you delete the URL, all you're doing is destroying your reference to that page - you're not deleting the actual page itself.

If I print out the page and give you the printout, I'm passing by value. Your page is a disconnected copy of the original. You won't see any subsequent changes, and any changes that you make (e.g. scribbling on your printout) will not show up on the original page. If you destroy the printout, you have actually destroyed your copy of the object - but the original web page remains intact.



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值