Sunday, December 11, 2011

C++ questions

1
2
3
4
5
6
7
8
9
10
11
12
13



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <string>
class Animal
{
protected:
    std::string m_strName;
    // We're making this constructor protected because
    // we don't want people creating Animal objects directly,
    // but we still want derived classes to be able to use it.
    Animal(std::string strName)
        : m_strName(strName)
    {
    }
public:
    std::string GetName() { return m_strName; }
    virtual const char* Speak() { return "???"; }
};
class Cat: public Animal
{
public:
    Cat(std::string strName)
        : Animal(strName)
    {
    }
    virtual const char* Speak() { return "Meow"; }
};
class Dog: public Animal
{
public:
    Dog(std::string strName)
        : Animal(strName)
    {
    }
    virtual const char* Speak() { return "Woof"; }
};



void Report(Animal &rAnimal) //这里不用&会怎么样?
{
    cout << rAnimal.GetName() << " says " << rAnimal.Speak() << endl;
}
int main()
{
    Cat cCat("Fred");
    Dog cDog("Garbo");
    Report(cCat);
    Report(cDog);
}

A word of warning: the signature of the derived class function must exactly
 match the signature of the base class virtual function in order for the derived 
class function to be used. If the derived class function has different parameter 
types, the program will likely still compile fine, but the virtual function will not 
resolve as intended.  ?这种情况属于overloading函数重载 还是override?


网上解释:
inherence就是继承啊,OO里的基本概念。
overide是指派生类中有一个和基类同名的函数(只要同名就算),我们就称“类B   override了类A的fun()成员函数”。
overload是指在同一个namespace中,有多个同名但不同参数的函数。不同的类之间不存在overload关系。
因此,override和overload是截然不同的:override发生于派生类与基类之间,而overload只发生于一个类或者namespace的范围之内。


overload和override的区别

override(重写)
1、方法名、参数、返回值相同。
2、子类方法不能缩小父类方法的访问权限。
3、子类方法不能抛出比父类方法更多的异常(但子类方法可以不抛出异常)。
4、存在于父类和子类之间。
5、方法被定义为final不能被重写。
overload(重载)1、参数类型、个数、顺序至少有一个不相同。 
2、不能重载只有返回值不同的方法名。
3、存在于父类和子类、同类中。


Question 2:

int main()
{
    Derived *pDerived = new Derived(5); //new 和 &的区别 为built-in types (eg. char, int, double, etc…)建立指针,用&,否则用new?
    Base *pBase = pDerived;
    delete pBase;
    return 0;
}


Question 3:
http://www.learncpp.com/cpp-tutorial/123-virtual-destructors-virtual-assignment-and-overriding-virtualization/

Overriding virtualization
Very rarely you may want to override the virtualization of a function. For example, consider the following code:
1
2
3
4
5
6
7
8
9
10
11
class Base
{
public:
    virtual const char* GetName() { return "Base"; }
};
class Derived: public Base
{
public
    virtual const char* GetName() { return "Derived"; }
};
There may be cases where you want a Base pointer to a Derived object to call Base::GetName() instead of Derived::GetName(). To do so, simply use the scope resolution operator:
1
2
3
4
5
6
7
int main()
{
    Derived cDerived;
    Base &rBase = cDerived;//Quetion: what's this?
    // Calls Base::GetName() instead of the virtualized Derived::GetName()
    cout << rBase.Base::GetName() << endl;
}
You probably won’t use this very often, but it’s good to know it’s at least possible.

http://www.learncpp.com/cpp-tutorial/142-function-template-instances/
Another example
Let’s do one more example of a function template. The following function template will calculate the average of a number of objects in an array:
1
2
3
4
5
6
7
8
9
10
template <class T>
T Average(T *atArray, int nNumValues) //这里为什么是T *atArray 而不用T &atArray
{
    T tSum = 0;
    for (int nCount=0; nCount < nNumValues; nCount++)
        tSum += atArray[nCount];
    tSum /= nNumValues;
    return tSum;
}
Now let’s see it in action:
1
2
3
4
5
int anArray[] = { 5, 3, 2, 1, 4 };
cout << Average(anArray, 5) << endl;
double dnArray[] = { 3.12, 3.45, 9.23, 6.34 };
cout << Average(dnArray, 4) << endl;
This produces the values:

3
5.535
QUESTION:
double& operator[](int nIndex)
    {
        assert(nIndex >= 0 && nIndex < m_nLength);
        return m_pdData[nIndex];
    }


http://www.learncpp.com/cpp-tutorial/143-template-classes/



No comments:

Post a Comment