C++ Basic Concept

1. Constructor & Destructor

Q: Is it possible to have Virtual Constructor? If yes, how? If not, Why not possible?

A: There is nothing like Virtual Constructor. The Constructor can’t be virtual as the constructor

is a code which is responsible for creating an instance of a class and it can’t be delegated to

any other object by virtual keyword means.

Q: What about Virtual Destructor?

A: Yes there is a Virtual Destructor. A destructor can be virtual as it is possible as at runtime

depending on the type of object caller is calling to, proper destructor will be called.

Q: What is copy constructor?

A: Constructor which initializes the it's object member variables ( by

shallow copying) with another object of the same class. If you don't implement one in your class

then compiler implements one for you. for example:

(a) Boo Obj1(10); // calling Boo constructor

(b) Boo Obj2(Obj1); // calling boo copy constructor

(c) Boo Obj2 = Obj1;// calling boo copy constructor

Q: When are copy constructors called?

A: Copy constructors are called in following cases:

(a) when a function returns an object of that

class by value

(b) when the object of that class is passed by

value as an argument to a function

(c) when you construct an object based on another

object of the same class

(d) When compiler generates a temporary object


2. Virtual

Q: What is virtual function?

A: When derived class overrides the base class method by redefining the same function, then if

client wants to access redefined the method from derived class through a pointer from base class

object, then you must define this function in base class as virtual function.

class parent?{

? ? void Show( )?{

? ? ? ? cout << "i'm parent" << endl;

? ? }

};

class child : ?public parent?{

? ? void Show( )?{

? ? ? ? cout << "i'm child" << endl;

}

};

parent * parent_object_ptr = new child;

parent_object_ptr->show() // calls parent->show()

now we goto virtual world...

class parent?{

? ? virtual void Show( )?{

? ? ? ? cout << "i'm parent" << endl;

? ? }

};

class child : public parent?{

? ? void Show( )?{

? ? ? ? cout << "i'm child" << endl;

? ? }

};

parent * parent_object_ptr = new child;

parent_object_ptr->show() // calls child->show()


Q: What is a "pure virtual" member function?

A: The abstract class whose pure virtual method has to be implemented by all the classes which

derive on these. Otherwise it would result in a compilation error. This construct should be used

when one wants to ensure that all the derived classes implement the method defined as pure

virtual in base class.

Q: How virtual functions are implemented C++?

A: Virtual functions are implemented using a table of function pointers, called the vtable. There

is one entry in the table per virtual function in the class. This table is created by the constructor

of the class. When a derived class is constructed, its base class is constructed _rst which creates

the vtable. If the derived class overrides any of the base classes virtual functions, those entries in

the vtable are overwritten by the derived class constructor. This is why you should never call

virtual functions from a constructor: because the vtable entries for the object may not have

been set up by the derived class constructor yet, so you might end up calling base class

implementations of those virtual functions

Q: What is pure virtual function? or what is abstract class?

A: When you de_ne only function prototype in a base class without implementation and do the

complete implementation in derived class. This base class is called abstract class and client won't

able to instantiate an object using this base class. You can make a pure virtual function or

abstract class this way..

class Boo?{

? ? void foo() = 0;

}

Boo MyBoo; // compilation error

Q: What is Pure Virtual Function? Why and when it is used?

A: The abstract class whose pure virtual method has to be implemented by all the classes which

derive on these. Otherwise it would result in a compilation error. This construct should be used

when one wants to ensure that all the derived classes implement the method defined as pure

virtual in base class.

3. Inheritance

Q: What is inheritance?

A: Inheritance allows one class to reuse the state and behavior of another class. The derived class

inherits the properties and method implementations of the base class and extends it by overriding

methods and adding additional properties and methods.

Q: What is multiple inheritance(virtual inheritance)? What are its advantages and disadvantages?

A: Multiple Inheritance is the process whereby a child can be derived from more than one parent

class. The advantage of multiple inheritance is that it allows a class to inherit the functionality of

more than one base class thus allowing for modeling of complex relationships.

The disadvantage of multiple inheritance is that it can lead to a lot of confusion(ambiguity)

when two base classes implement a method with the same name.

4. Polymorphism

Q: What is Polymorphism?

A: Polymorphism allows a client to treat different objects in the same way even if they were

created from different classes and exhibit different behaviors. You can use implementation

inheritance to achieve polymorphism in languages such as C++ and Java. Base class object's

pointer can invoke methods in derived class objects. You can also achieve polymorphism in

C++ by function overloading and operator overloading.

5. Class

Q: What are the differences between a C++ struct and C++ class?

A: The default member and base class access specifies are different. This is one of the

commonly misunderstood aspects of C++. Believe it or not, many programmers think that a C++

struct is just like a C struct, while a C++ class has inheritance, access specifes, member

functions, overloaded operators, and so on. Actually, the C++ struct has all the features of the

class. The only differences are that a struct defaults to public member access and public base

class inheritance, and a class defaults to the private access specified and private base-class

inheritance.

Q: What is encapsulation?

A: Containing and hiding Information about an object, such as internal data structures and code.

Encapsulation isolates the internal complexity of an object's operation from the rest of the

application. For example, a client component asking for net revenue from a business object need

not know the data's origin.

Q: What is Overriding?

A: To override a method, a subclass of the class that originally declared the method must declare

a method with the same name, return type (or a subclass of that return type), and same parameter?list.

The definition of the method overriding is:

· Must have same method name.

· Must have same data type.

· Must have same argument list.

Overriding a method means that replacing a method functionality in child class. To imply

overriding functionality we need parent and child classes. In the child class you define the same

method signature as one defined in the parent class.


6. ?Memory Allocation/ Deallocation

Q: What is the difference between new/delete and malloc/free?

A: malloc allocates memory for object in heap but doesn't invoke object's constructor to initialize

the object. new allocates memory and also invokes constructor to initialize the object. malloc()

and free() do not support object semantics, does not construct and destruct objects

Eg. string * ptr = (string *)(malloc (sizeof(string))) Are not safe, and does not calculate the size

of the objects that it construct

The following return a pointer to void

int *p = (int *) (malloc(sizeof(int)));

int *p = new int;

Are not extensible

new and delete can be overloaded in a class

"delete" first calls the object's termination routine (i.e. its destructor) and then releases the space

the object occupied on the heap memory. If an array of objects was created using new, then

delete must be told that it is dealing with an array by preceding the name with an empty []:-

Int_t *my_ints = new Int_t[10];

...

delete []my_ints;

Q: What is the difference between delete and delete[]?

A: Whenever you allocate memory with new[], you have to free the memory using delete[].

When you allocate memory with 'new', then use 'delete' without the brackets. You use new[] to

allocate an array of values (always starting at the index 0).


7. Pointer

Q: What is the difference between a pointer and a reference?

A: A reference must always refer to some object and, therefore, must always be initialized;

pointers do not have such restrictions. A pointer can be reassigned to point to different objects

while a reference always refers to an object with which it was initialized.

Q: When should I use references, and when should I use pointers?

A: Use references when you can, and pointers when you have to.

References are usually preferred over pointers whenever you don't need "reseating". This usually

means that references are most useful in a class's public interface. References typically appear on

the skin of an object, and pointers on the inside.

The exception to the above is where a function's parameter or return value needs a "sentinel"

reference a reference that does not refer to an object. This is usually best done by

returning/taking a pointer, and giving the NULL pointer this special significance (references

should always alias objects, not a dereferenced NULL pointer).

Note: Old line C programmers sometimes don't like references since they provide reference

semantics that isn't explicit in the caller's code. After some C++ experience, however, one

quickly realizes this is a form of information hiding, which is an asset rather than a liability. E.g.,

programmers should write code in the language of the problem rather than the language of the

machine.

Q: What is a smart pointer?

A: A smart pointer is a C++ class that mimics a regular pointer in syntax and some semantics,

but it does more. Because smart pointers to different types of objects tend to have a lot of code in?common, almost all good-quality smart pointers in existence are templated by the pointee type.

Q: What is auto pointer?

A: The simplest example of a smart pointer is auto_ptr, which is included in the standard C++

library. Auto Pointer only takes care of Memory leak and does nothing about dangling pointers

issue. You can find it in the header . Here is part of auto_ptr's implementation.

8. Overload

Q: What is overloading??

A: With the C++ language, you can overload functions and operators. Overloading is the

practice of supplying more than one definition for a given function name in the same scope.

- Any two functions in a set of overloaded functions must have different argument lists.

- Overloading functions with argument lists of the same types, based on return type alone, is an

error.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末年缎,一起剝皮案震驚了整個(gè)濱河市纽疟,隨后出現(xiàn)的幾起案子对供,更是在濱河造成了極大的恐慌磷脯,老刑警劉巖聂使,帶你破解...
    沈念sama閱讀 222,627評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異丢早,居然都是意外死亡板鬓,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,180評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門覆履,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)蹋盆,“玉大人费薄,你說(shuō)我怎么就攤上這事∑芪恚” “怎么了楞抡?”我有些...
    開(kāi)封第一講書(shū)人閱讀 169,346評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)析藕。 經(jīng)常有香客問(wèn)我召廷,道長(zhǎng),這世上最難降的妖魔是什么账胧? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 60,097評(píng)論 1 300
  • 正文 為了忘掉前任柱恤,我火速辦了婚禮,結(jié)果婚禮上找爱,老公的妹妹穿的比我還像新娘梗顺。我一直安慰自己,他們只是感情好车摄,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,100評(píng)論 6 398
  • 文/花漫 我一把揭開(kāi)白布寺谤。 她就那樣靜靜地躺著,像睡著了一般吮播。 火紅的嫁衣襯著肌膚如雪变屁。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 52,696評(píng)論 1 312
  • 那天意狠,我揣著相機(jī)與錄音粟关,去河邊找鬼。 笑死环戈,一個(gè)胖子當(dāng)著我的面吹牛闷板,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播院塞,決...
    沈念sama閱讀 41,165評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼遮晚,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了拦止?” 一聲冷哼從身側(cè)響起县遣,我...
    開(kāi)封第一講書(shū)人閱讀 40,108評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎汹族,沒(méi)想到半個(gè)月后萧求,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,646評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡顶瞒,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,709評(píng)論 3 342
  • 正文 我和宋清朗相戀三年夸政,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片搁拙。...
    茶點(diǎn)故事閱讀 40,861評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡秒梳,死狀恐怖法绵,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情酪碘,我是刑警寧澤朋譬,帶...
    沈念sama閱讀 36,527評(píng)論 5 351
  • 正文 年R本政府宣布,位于F島的核電站兴垦,受9級(jí)特大地震影響徙赢,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜探越,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,196評(píng)論 3 336
  • 文/蒙蒙 一狡赐、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧钦幔,春花似錦枕屉、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 32,698評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至卷玉,卻和暖如春哨颂,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背相种。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,804評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工威恼, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人寝并。 一個(gè)月前我還...
    沈念sama閱讀 49,287評(píng)論 3 379
  • 正文 我出身青樓箫措,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親食茎。 傳聞我的和親對(duì)象是個(gè)殘疾皇子蒂破,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,860評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容