CC++ 面试题集锦英文版(2)

2019-01-19 18:13

POLYMORPHISM : A phenomenon which enables an object to react differently to the same function call.

in C++ it is attained by using a keyword virtual Example

public class SHAPE {

public virtual void SHAPE::DRAW()=0; }

Note here the function DRAW() is pure virtual which means the sub classes must implement the DRAW() method and SHAPE cannot be instatiated public class CIRCLE::public SHAPE {

public void CIRCLE::DRAW() {

// TODO drawing circle } }

public class SQUARE::public SHAPE {

public void SQUARE::DRAW() {

// TODO drawing square } }

now from the user class the calls would be like globally

SHAPE *newShape;

When user action is to draw

public void MENU::OnClickDrawCircle(){ newShape = new CIRCLE(); }

public void MENU::OnClickDrawCircle(){ newShape = new SQUARE(); }

the when user actually draws

public void CANVAS::OnMouseOperations(){ newShape->DRAW(); }

Answer2

class SHAPE{

public virtual Draw() = 0; //abstract class with a pure virtual method };

class CIRCLE{ public int r;

public virtual Draw() { this->drawCircle(0,0,r); } };

class SQURE public int a;

public virtual Draw() { this->drawRectangular(0,0,a,a); } };

Each object is driven down from SHAPE implementing Draw() function in its own way. What is an object?

Object is a software bundle of variables and related methods. Objects have state and behavior. How can you tell what shell you are running on UNIX system?

You can do the Echo $RANDOM. It will return a undefined variable if you are from the C-Shell, just a return prompt if you are from the Bourne shell, and a 5 digit random numbers if you are from the Korn shell. You could also do a ps -l and look for the shell with the highest PID. Describe PRIVATE, PROTECTED and PUBLIC – the differences and give examples.

class Point2D{ int x; int y; public int color;

protected bool pinned;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor };

Point2D MyPoint;

You cannot directly access private data members when they are declared (implicitly) private: MyPoint.x = 5; // Compiler will issue a compile ERROR //Nor yoy can see them:

int x_dim = MyPoint.x; // Compiler will issue a compile ERROR On the other hand, you can assign and read the public data members: MyPoint.color = 255; // no problem int col = MyPoint.color; // no problem

With protected data members you can read them but not write them: MyPoint.pinned = true; // Compiler will issue a compile ERROR

bool isPinned = MyPoint.pinned; // no problem What is namespace?

Namespaces allow us to group a set of global classes, objects and/or functions under a name. To say it somehow, they serve to split the global scope in sub-scopes known as namespaces. The form to use namespaces is:

namespace identifier { namespace-body }

Where identifier is any valid identifier and namespace-body is the set of classes, objects and functions that are included within the namespace. For example:

namespace general { int a, b; } In this case, a and b are normal variables integrated within the general namespace. In order to access to these variables from outside the namespace we have to use the scope operator ::. For example, to access the previous variables we would have to put: general::a general::b

The functionality of namespaces is specially useful in case that there is a possibility that a global

object or function can have the same name than another one, causing a redefinition error. What do you mean by inheritance?

Inheritance is the process of creating new classes, called derived classes, from existing classes or base classes. The derived class inherits all the capabilities of the base class, but can add embellishments and refinements of its own.

What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a method that accepts an object of the same class and copies it‘s data members to the object on the left part of assignement: class Point2D{ int x; int y; public int color;

protected bool pinned;

public Point2D() : x(0) , y(0) {} //default (no argument) constructor public Point2D( const Point2D & ) ; };

Point2D::Point2D( const Point2D & p ) {

this->x = p.x; this->y = p.y;

this->color = p.color; this->pinned = p.pinned; }

main(){

Point2D MyPoint; MyPoint.color = 345;

Point2D AnotherPoint = Point2D( MyPoint ); // now AnotherPoint has color = 345 What is Boyce Codd Normal form?

A relation schema R is in BCNF with respect to a set F of functional dependencies if for all functional dependencies in F+ of the form a-> , where a and b is a subset of R, at least one of the following holds:

* a- > b is a trivial functional dependency (b is a subset of a) * a is a superkey for schema R

What is virtual class and friend class?

Friend classes are used when two or more classes are designed to work together and need access to each other‘s implementation in ways that the rest of the world shouldn‘t be allowed to have. In other words, they help keep private things private. For instance, it may be desirable for class DatabaseCursor to have more privilege to the internals of class Database than main() has.

What is the word you will use when defining a function in base class to allow this function to be a polimorphic function? virtual

What do you mean by binding of data and functions? Encapsulation.

What are 2 ways of exporting a function from a DLL?

1.Taking a reference to the function from the DLL instance. 2. Using the DLL ‘s Type Library.

What is the difference between an object and a class?

Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

- A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don‘t change.

- The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.

- An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

Suppose that data is an array of 1000 integers. Write a single function call that will sort the 100 elements data [222] through data [321]. quicksort ((data + 222), 100); What is a class?

Class is a user-defined data type in C++. It can be created to solve a particular kind of problem. After creation the user need not know the specifics of the working of a class. What is friend function?

As the name suggests, the function acts as a friend to a class. As a friend of a class, it can access its private and protected members. A friend function is not a member of the class. But it must be listed in the class definition.

Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?

Mergesort always makes recursive calls to sort subarrays that are about half size of the original array, resulting in O(n log n) time. What is abstraction?

Abstraction is of the process of hiding unwanted details from the user. What are virtual functions?

A virtual function allows derived classes to replace the implementation provided by the base class. The compiler makes sure the replacement is always called whenever the object in question is actually of the derived class, even if the object is accessed by a base pointer rather than a derived pointer. This allows algorithms in the base class to be replaced in the derived class, even if users don‘t know about the derived class.

What is the difference between an external iterator and an internal iterator? Describe an advantage of an external iterator.

An internal iterator is implemented with member functions of the class that has items to step through. .An external iterator is implemented as a separate class that can be ―attach‖ to the object that has items to step through. .An external iterator has the advantage that many difference iterators can be active simultaneously on the same object. What is a scope resolution operator?

A scope resolution operator (::), can be used to define the member functions of a class outside the class.

What do you mean by pure virtual functions?

A pure virtual member function is a member function that the base class forces derived classes to provide. Normally these member functions have no implementation. Pure virtual functions are equated to zero.

class Shape { public: virtual void draw() = 0; }; What is polymorphism? Explain with an example?

“Poly‖ means ―many‖ and ―morph‖ means ―form‖. Polymorphism is the ability of an object (or reference) to assume (be replaced by) or become many different forms of object.

Example: function overloading, function overriding, virtual functions. Another example can be a plus ?+‘ sign, used for adding two integers or for using it to concatenate two strings.

How can you quickly find the number of elements stored in a a) static array b) dynamic array ? Why is it difficult to store linked list in an array?

How can you find the nodes with repetetive data in a linked list?

Write a prog to accept a given string in any order and flash error if any of the character is different. For example : If abc is the input then abc, bca, cba, cab bac are acceptable but aac or bcd are unacceptable.

Write out a function that prints out all the permutations of a string. For example, abc would give you abc, acb, bac, bca, cab, cba. You can assume that all the characters will be unique. What‘s the output of the following program? Why? #include main() {

typedef union { int a;

char b[10]; float c; }

Union;

Union x,y = {100}; x.a = 50;

strcpy(x.b,\\‖hello\\‖); x.c = 21.50;

printf(\\‖Union x : %d %s %f \\n\\‖,x.a,x.b,x.c ); printf(\\‖Union y :%d %s%f \\n\\‖,y.a,y.b,y.c); }

Given inputs X, Y, Z and operations | and & (meaning bitwise OR and AND, respectively) What is output equal to in

output = (X & Y) | (X & Z) | (Y & Z)

Why are arrays usually processed with for loop?

The real power of arrays comes from their facility of using an index variable to traverse the array, accessing each element with the same expression a[i]. All the is needed to make this work is a iterated statement in which the variable i serves as a counter, incrementing from 0 to a.length -1. That is exactly what a loop does. What is an HTML tag?


CC++ 面试题集锦英文版(2).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:中山市2011年高三教研试题(理数)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: