The key idea behind OOP is polymorphism. As we'll see, in C++, polymorphism applies only to references or pointers to types related by inheritance.
Inheritance lets us define classes that model relationships among types, sharing what is common and specializing only that which is inherently different.
Dynamic binding lets us write programs that use objects of any type in an inheritance hierarchy without caring about the objects' specific types.
Note: In C++, dynamic binding happens when a virtual function is called through a reference (or a pointer) to a base class. The fact that a reference (or pointer) might refer to either a base- or a derived-class object is the key to dynamic binding. Calls to virtual functions made through a reference (or pointer) are resolved at run time: The function that is called is the one defined by the actual type of the object to which the reference (or pointer) refers.
Virtual destructor It is worth noting that classes used as the root class of an inheritance hierarchy generally define a virtual destructor.
Virtual functions
1) Purpose: The purpose of the virtual keyword is to enable dynamic binding. By default, member functions are nonvirtual. Calls to nonvirtual functions are resolved at compile time.
2) Rule: A base class usually should define as virtual any function that a derived class will need to redefine.
3) Scope: Any nonstatic member function, other than a constructor, may be virtual. The virtual keyword appears only on the member-function declaration inside the class. The virtual keyword may not be used on a function definition that appears outside the class body.
Virtual function in derived class: Once a function is declared as virtual in a base class it remains virtual; nothing the derived classes do can change the fact that the function is virtual. When a derived class redefines a virtual, it may use the virtual keyword, but it is not required to do so.
Inheritance vs Composition:
Inheritance should reflect a so-called "Is A" relationship of the derived class to the base class. In our bookstore example, our base class represents the concept of a book sold at a stipulated price. Our Bulk_item is a kind of book, but one with a different pricing strategy.
Composition among types is a so-called "Has A" relationship. Our bookstore classes have a price and they have an ISBN. Types related by a "Has A" relationship imply membership. Thus, our bookstore classes are composed from members representing the price and the ISBN.
Operator Overloading and Friendship
To support:
cout << item1 + item2 << endl;
we need the following definition:
class Sales_item {
friend std::istream& operator>>
(std::istream&, Sales_item&);
friend std::ostream& operator<<
(std::ostream&, const Sales_item&);
public:
Sales_item& operator+=(const Sales_item&);
};
Sales_item operator+(const Sales_item&, const Sales_item&);
Wednesday, October 8, 2008
Tuesday, October 7, 2008
Inside the C++ Object Model
Although you can manipulate a base class object of an inheritance hierarchy either directly or indirectly, only the indirect manipulation of the object through a pointer or reference supports the polymorphism necessary for OO programming.
Two ways to manipulate a base class object of a hierarchy of classes:
1) directly through base class
BaseA a;
a.DoSomething()
2) Indirectly using a pointer or reference
DerivedB b;
BaseA& a = b;
BaseA* c = &b;
Only the latter supports the polymorphism necessary for OO programming.
In the OO paradigm, the programmer manipulates an unknown instance of a bounded but infinite set of types. The actual type of the object addressed is not resolved in principle until runtime at each particular point of execution. In C++, this is achieved only through the manipulation of objects through pointers and references. In contrast, in the ADT paradigm the programmer manipulates an instance of a fixed, singular type that is completely defined at the point of compilation.
Two ways to manipulate a base class object of a hierarchy of classes:
1) directly through base class
BaseA a;
a.DoSomething()
2) Indirectly using a pointer or reference
DerivedB b;
BaseA& a = b;
BaseA* c = &b;
Only the latter supports the polymorphism necessary for OO programming.
In the OO paradigm, the programmer manipulates an unknown instance of a bounded but infinite set of types. The actual type of the object addressed is not resolved in principle until runtime at each particular point of execution. In C++, this is achieved only through the manipulation of objects through pointers and references. In contrast, in the ADT paradigm the programmer manipulates an instance of a fixed, singular type that is completely defined at the point of compilation.
Monday, October 6, 2008
google面试题(zz)
发信人: justcome (ANewOne), 信区: JobHunting
标 题: 问一道google面试题
发信站: BBS 未名空间站 (Mon Oct 6 17:54:07 2008), 转信
没有看懂答案是什么意思?能不能给解释一下
3. you need read a lot of records, you don't know how many records here
before you complete it. After you read all those records, you need select
one record randomly.In another words, every record has same chance to be
selected. The memory is limited, that means you cannot store all those
records at one time.
Changing your random number pick dynamically. Add a weight (1/n) to nth
new number, add a weight (n-1)/n to the random number you picked from (1 to
n-1) numbers. This can guaranteer the randomess without knowing how many
number ahead.
1. P(1)=1; 2. P(2)=1/2, P(1)=1*1/2; 3. P(3)=1/3, P(1)=P(2)=2/3*1/2=1/3…
标 题: 问一道google面试题
发信站: BBS 未名空间站 (Mon Oct 6 17:54:07 2008), 转信
没有看懂答案是什么意思?能不能给解释一下
3. you need read a lot of records, you don't know how many records here
before you complete it. After you read all those records, you need select
one record randomly.In another words, every record has same chance to be
selected. The memory is limited, that means you cannot store all those
records at one time.
Changing your random number pick dynamically. Add a weight (1/n) to nth
new number, add a weight (n-1)/n to the random number you picked from (1 to
n-1) numbers. This can guaranteer the randomess without knowing how many
number ahead.
1. P(1)=1; 2. P(2)=1/2, P(1)=1*1/2; 3. P(3)=1/3, P(1)=P(2)=2/3*1/2=1/3…
Sunday, October 5, 2008
memory layouts of C++ classes (zz)
Newsgroups: comp.compilers
From: ssimmons@convex.com (Steve Simmons)
Keywords: C++, code, design, comment
Organization: CONVEX News Network, Engineering (cnn.eng), Richardson, Tx USA
References: 94-08-087
Date: Sun, 14 Aug 1994 15:48:29 GMT
> After walking the heap to reverse engineer the memory layout
> of a class in MFC, I figured this forum might provide a better
> answer. How does a C++ compiler build the memory layout of a class
> and a derived class?
First, memory layout is not defined by the language... It is an
implementation issue; however, a common approach is the following...
Given:
Class A{
field a;
}
Class B{
field b;
}
Class C{
field c;
}
No Inheritance:
For class A, the layout would be:
first field: pointer to virtual table A
second field: the field a.
Single Inheritance:
For B derived from A (class B : public A), the layout would be:
first field: pointer to virtual table for B... first
entries in this table are inherited/overriden
virtual functions of A.
second field: the field a.
third field: the field b.
Multiple Inheritance:
For C derived from both A and B (class C : public A, public B), the
layout would be:
first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.
second field: the field a.
third field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.
fourth field: the field a in B's class.
fifth field the field b
sixth field: the field c.
Virtual Inheritance:
As you can see in the above example, field A is replicated twice in
Class C. If you want only only one field A, you would need to
do the following:
class A
class B : public virtual A
class C : public virtual A, public B
The layout for C would be the following:
first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.
second field: pointer to field a.
third field: the field a. (points to second field)
fourth field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.
fifth field: pointer to field a (should point to second field)
sixth field: field a (never used, cheaper to allocate here as
opposed to doing a malloc).
seventh field the field b
eighth field: the field c.
> Also, what is the difference in memory layout
> of a regular C++ compiler and the memory layouts used by SOM compilers
> to provide binary consistency?
This would be interesting to hear... does someone out there know???
Thank you.
Steve Simmons
[What are SOM compilers? -John]
From: ssimmons@convex.com (Steve Simmons)
Keywords: C++, code, design, comment
Organization: CONVEX News Network, Engineering (cnn.eng), Richardson, Tx USA
References: 94-08-087
Date: Sun, 14 Aug 1994 15:48:29 GMT
> After walking the heap to reverse engineer the memory layout
> of a class in MFC, I figured this forum might provide a better
> answer. How does a C++ compiler build the memory layout of a class
> and a derived class?
First, memory layout is not defined by the language... It is an
implementation issue; however, a common approach is the following...
Given:
Class A{
field a;
}
Class B{
field b;
}
Class C{
field c;
}
No Inheritance:
For class A, the layout would be:
first field: pointer to virtual table A
second field: the field a.
Single Inheritance:
For B derived from A (class B : public A), the layout would be:
first field: pointer to virtual table for B... first
entries in this table are inherited/overriden
virtual functions of A.
second field: the field a.
third field: the field b.
Multiple Inheritance:
For C derived from both A and B (class C : public A, public B), the
layout would be:
first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.
second field: the field a.
third field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.
fourth field: the field a in B's class.
fifth field the field b
sixth field: the field c.
Virtual Inheritance:
As you can see in the above example, field A is replicated twice in
Class C. If you want only only one field A, you would need to
do the following:
class A
class B : public virtual A
class C : public virtual A, public B
The layout for C would be the following:
first field: pointer to virtual table for C... first
entries in this table are inherited/overriden
virtual functions of A.
second field: pointer to field a.
third field: the field a. (points to second field)
fourth field: pointer to virtual for C...first
entries in this table are inherited/overriden
virtual functions of B.
fifth field: pointer to field a (should point to second field)
sixth field: field a (never used, cheaper to allocate here as
opposed to doing a malloc).
seventh field the field b
eighth field: the field c.
> Also, what is the difference in memory layout
> of a regular C++ compiler and the memory layouts used by SOM compilers
> to provide binary consistency?
This would be interesting to hear... does someone out there know???
Thank you.
Steve Simmons
[What are SOM compilers? -John]
告别我的跳槽岁月(ZZ)
发信人: ctmj (James), 信区: JobHunting
标 题: 告别我的跳槽岁月
发信站: BBS 未名空间站 (Sun Oct 5 12:23:49 2008)
在新公司工作已经有一段时间了。一直想写点东西总结一下找工跳槽的得失成败。一是
对三个月的找工时日做个总结,二也希望对其他人有所帮助。
我的背景:
MBA毕业后在一家Consulting公司工作了快五年,决定换工作前刚拿到绿卡。
第一,确认自己的职业目标。个人感觉这一步是最难但又必须考虑清楚的,因为找工作
切记要专注。可能由于我是学MBA和做咨询的缘故,感觉上啥都可以做:Strategic
Development, Finance, Venture Capital etc. 但又很难说的很清晰、具体。刚开始
的时候我是见了什么职位sexy就投什么,深感效果不彰。但慢慢在找工作的过程当中对
自己以及就业市场有了比较客观的认识,也就逐渐确立了职业目标。其实这也就是个小
马过河的问题 --- 有些事情可以找很多人了解,但每个人的情形都不大一样,所以必
须自己试一试才知道。
第二,简历:多读、多写、多改。我写简历的思路大致是这样的 --- 首先拿自己以前
的简历好好看看,总结一下自己最近的一些工作经历。然后换位从Recruiter的角度来
想:什么样的经历和Skills会引起Recruiter的兴趣?如何包装自己的经历?谈到包装
,我觉得首先要诚实、有一说一。但也要学会组织语言去突出自己的优点、长项。我一
直认为每个人都有独特的背景和优点,关键是如何去更好的通过简历去展现自己。这个
问题往往不是一天两天能够回答的。所以说写简历是一个周而复始的过程,不可能once
and for all 的一次搞定。
第三,找工作的渠道,根据我的经历按有效程度排列如下:
第一, 猎头公司。可能由于我有工作经历的缘故,我的几个offer都是通过猎头找
到的。有的Recruiter相当不错,分析你的背景,寻找合适的机会,帮助准备面试。但
有的Recruiter也很Jerk,这种人就不理他就是了。
第二, 上网贴简历。网上贴简历有两个好处,一个是吸引好的猎头和你联系;其次
一些大公司也会主动在网上找人,有两家大公司就是这么和我联系上的。
第三, 同学、朋友帮忙。我觉得这点被很多人过分强调了。同学、朋友有相当作用
,尤其是在定位及准备的过程当中。但找工的时候很难碰上合适的机会。
第四, 上公司网站递简历。费时费力,往往填一个公司的网页要半到一个小时,而
且填完了感觉基本没人看。我一般是等拿到offer或者HR的老大一定要填的时候才填。
第五, 回应招聘网站(Monster, CareerBuilder etc.)的招聘启事。就我个人而
言,基本没啥用。
第四,如何准备面试:精心准备,勤于苦练。记得刚开始找工作的过程,一个Bank有个
挺不错的机会。我实际是在mentally没有ready的情况下就去匆匆面试了,结果就不言
而喻了。正所谓机会是给有准备的人的。后来这家Bank没有能挺过这次金融危机,但那
是后话了。
第五,Offer Negotiation.
最近market很不好,我实际上也没有太多的leverage去negotiate。所以只是选了一个
还不错的offer然后counter-offer了一次就从了。
现在金融市场很不好,各行各业也受到很大影响,找工作也变得很艰难了。而作为中国
学生又有身份、语言、背景各方面的障碍,找工之路会尤其艰辛。希望大家能够咬牙顶
住,希望就在前方。
标 题: 告别我的跳槽岁月
发信站: BBS 未名空间站 (Sun Oct 5 12:23:49 2008)
在新公司工作已经有一段时间了。一直想写点东西总结一下找工跳槽的得失成败。一是
对三个月的找工时日做个总结,二也希望对其他人有所帮助。
我的背景:
MBA毕业后在一家Consulting公司工作了快五年,决定换工作前刚拿到绿卡。
第一,确认自己的职业目标。个人感觉这一步是最难但又必须考虑清楚的,因为找工作
切记要专注。可能由于我是学MBA和做咨询的缘故,感觉上啥都可以做:Strategic
Development, Finance, Venture Capital etc. 但又很难说的很清晰、具体。刚开始
的时候我是见了什么职位sexy就投什么,深感效果不彰。但慢慢在找工作的过程当中对
自己以及就业市场有了比较客观的认识,也就逐渐确立了职业目标。其实这也就是个小
马过河的问题 --- 有些事情可以找很多人了解,但每个人的情形都不大一样,所以必
须自己试一试才知道。
第二,简历:多读、多写、多改。我写简历的思路大致是这样的 --- 首先拿自己以前
的简历好好看看,总结一下自己最近的一些工作经历。然后换位从Recruiter的角度来
想:什么样的经历和Skills会引起Recruiter的兴趣?如何包装自己的经历?谈到包装
,我觉得首先要诚实、有一说一。但也要学会组织语言去突出自己的优点、长项。我一
直认为每个人都有独特的背景和优点,关键是如何去更好的通过简历去展现自己。这个
问题往往不是一天两天能够回答的。所以说写简历是一个周而复始的过程,不可能once
and for all 的一次搞定。
第三,找工作的渠道,根据我的经历按有效程度排列如下:
第一, 猎头公司。可能由于我有工作经历的缘故,我的几个offer都是通过猎头找
到的。有的Recruiter相当不错,分析你的背景,寻找合适的机会,帮助准备面试。但
有的Recruiter也很Jerk,这种人就不理他就是了。
第二, 上网贴简历。网上贴简历有两个好处,一个是吸引好的猎头和你联系;其次
一些大公司也会主动在网上找人,有两家大公司就是这么和我联系上的。
第三, 同学、朋友帮忙。我觉得这点被很多人过分强调了。同学、朋友有相当作用
,尤其是在定位及准备的过程当中。但找工的时候很难碰上合适的机会。
第四, 上公司网站递简历。费时费力,往往填一个公司的网页要半到一个小时,而
且填完了感觉基本没人看。我一般是等拿到offer或者HR的老大一定要填的时候才填。
第五, 回应招聘网站(Monster, CareerBuilder etc.)的招聘启事。就我个人而
言,基本没啥用。
第四,如何准备面试:精心准备,勤于苦练。记得刚开始找工作的过程,一个Bank有个
挺不错的机会。我实际是在mentally没有ready的情况下就去匆匆面试了,结果就不言
而喻了。正所谓机会是给有准备的人的。后来这家Bank没有能挺过这次金融危机,但那
是后话了。
第五,Offer Negotiation.
最近market很不好,我实际上也没有太多的leverage去negotiate。所以只是选了一个
还不错的offer然后counter-offer了一次就从了。
现在金融市场很不好,各行各业也受到很大影响,找工作也变得很艰难了。而作为中国
学生又有身份、语言、背景各方面的障碍,找工之路会尤其艰辛。希望大家能够咬牙顶
住,希望就在前方。
分享一些我学到的面试技巧(ZZ)
发信人: dongyan (bird), 信区: JobHunting
标 题: 分享一些我学到的面试技巧
发信站: BBS 未名空间站 (Fri Oct 3 12:50:54 2008)
在本版潜水很久受益匪浅,现回报本版几个从Professional那学来的Interview技巧,
希望大家都能拿到心仪的offer:
1 What are the real reasons you left your job
回答问题要客观,不要带有感情色彩,回答原因后迅速转到自己正在寻找什么样的机会,比如我是laid off,那么我的回答就是: Due to the downsize of ** company, lots of positions were eliminated including mine. Now, I am looking for an opportunity in ****
Professional说不要用 My former employer,用自己former employer的名字,听起
来更客观,更职业。
2 Tell me about yourself.
在回答这个问题的时候,要偏重与该工作有关的经历。不要过长,控制在2分钟之内。刚开始,我不知道,还会说一些personality有关的故事。同样在别人问你的强处的时候,也该更多说一些与工作有关的强处。
3 讨论薪水。
一定要先appreciate offer,表示自己的interest,再谈要害。
4 简历的问题,帮我改简历的机构把我最开始的Objective去掉了。他们说没人care你想要什么工作,他们更care你会干什么。所以,我把Objective变成自己经验的一个总结。后面跟的是与工作有关的技能总结,就是大家常用的summary。
帮我改简历的机构还是把我的简历压缩成一页半了。但是我以前的老板说要写的详细,越细越好,也许不同的公司喜欢的风格不同吧。
5 Wrap up找工作
Professional说不到上班那一天就不要停止jobhunting(但是我挺懒的,拿到心仪的offer还在背景调查就不找了) 开始上班后,要给所有在找工作过程中帮过忙的人发一封信告知他们你的status并表示感谢。
码字很累,版主如果觉得文章还有意义,就发我几个包子吧^@^
改简历服务:www.lhh.com
他们给我改的简历结构就是
1 工作经验总结,more Like strength
2 skills
3 experience, not laundry list, mention your achievements.
4 education
标 题: 分享一些我学到的面试技巧
发信站: BBS 未名空间站 (Fri Oct 3 12:50:54 2008)
在本版潜水很久受益匪浅,现回报本版几个从Professional那学来的Interview技巧,
希望大家都能拿到心仪的offer:
1 What are the real reasons you left your job
回答问题要客观,不要带有感情色彩,回答原因后迅速转到自己正在寻找什么样的机会,比如我是laid off,那么我的回答就是: Due to the downsize of ** company, lots of positions were eliminated including mine. Now, I am looking for an opportunity in ****
Professional说不要用 My former employer,用自己former employer的名字,听起
来更客观,更职业。
2 Tell me about yourself.
在回答这个问题的时候,要偏重与该工作有关的经历。不要过长,控制在2分钟之内。刚开始,我不知道,还会说一些personality有关的故事。同样在别人问你的强处的时候,也该更多说一些与工作有关的强处。
3 讨论薪水。
一定要先appreciate offer,表示自己的interest,再谈要害。
4 简历的问题,帮我改简历的机构把我最开始的Objective去掉了。他们说没人care你想要什么工作,他们更care你会干什么。所以,我把Objective变成自己经验的一个总结。后面跟的是与工作有关的技能总结,就是大家常用的summary。
帮我改简历的机构还是把我的简历压缩成一页半了。但是我以前的老板说要写的详细,越细越好,也许不同的公司喜欢的风格不同吧。
5 Wrap up找工作
Professional说不到上班那一天就不要停止jobhunting(但是我挺懒的,拿到心仪的offer还在背景调查就不找了) 开始上班后,要给所有在找工作过程中帮过忙的人发一封信告知他们你的status并表示感谢。
码字很累,版主如果觉得文章还有意义,就发我几个包子吧^@^
改简历服务:www.lhh.com
他们给我改的简历结构就是
1 工作经验总结,more Like strength
2 skills
3 experience, not laundry list, mention your achievements.
4 education
Bloomberg的面试
1. stl, map,list啥的结构
2. 基于2叉树的遍历查找删除
3. C++继承
4. 一个bruteforce的算法
5. malloc/new的区别
6. new之后free有啥结果
7. 进程,线程的堆
8. pthread
9. virtual class
10. 全是函数的类的对象的size
11. hashmap做不了啥map能做的
12. os缺页之后硬件会trap
13.还有很多,记不得了,2轮面了将近2个小时,最后面完直接告诉我good luck,应该
没戏了
2. 基于2叉树的遍历查找删除
3. C++继承
4. 一个bruteforce的算法
5. malloc/new的区别
6. new之后free有啥结果
7. 进程,线程的堆
8. pthread
9. virtual class
10. 全是函数的类的对象的size
11. hashmap做不了啥map能做的
12. os缺页之后硬件会trap
13.还有很多,记不得了,2轮面了将近2个小时,最后面完直接告诉我good luck,应该
没戏了
Subscribe to:
Posts (Atom)