C++

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is an Explicit Constructor?

A conversion constructor is declared with the explicit keyword. The compiler does not use an explicit constructor to implement an implied conversion of types. Its purpose is reserved explicitly for construction.

What is the keyword auto for?

By default, every local variable of the function is automatic i.e. auto. In the below function both the variables 'i' and 'j' are automatic variables. void f() { int i; auto int j; } NOTE: A global variable is not an automatic variable.

What are the benefits of Operator Overloading?

By overloading standard operators on a class, we can extend the meaning of these operators, so that they can also operate on the other user-defined objects. Function overloading allows us to reduce the complexity of the code and make it more clear and readable as we can have the same function names with different argument lists.

What is a friend function?

C++ class does not allow its private and protected members to be accessed outside the class. But this rule can be violated by making use of the "Friend" function. As the name itself suggests, friend function is an external function which is a friend of the class. For friend function to access the private and protected methods of the class, we should have a prototype of the friend function with the keyword "friend" included inside the class.

What is the purpose of Extern Storage Specifier?

"Extern" specifier is used to resolve the scope of a global symbol. #include <iostream > using nam espace std; main() { extern int i; cout<<i<<endl; } int i=20; In the above code, "i" can be visible outside the file where it is defined.

Explain the ISA and HASA class relationships. How would you implement each?

"ISA" relationship usually exhibits inheritance as it implies that a class "ISA" specialized version of another class. Example, An employee ISA person. That means an Employee class is inherited from the Person class. Contrary to "ISA", "HASA" relationship depicts that an entity may have another entity as its member or a class has another object embedded inside it. So taking the same example of an Employee class, the way in which we associate the Salary class with the employee is not by inheriting it but by including or containing the Salary object inside the Employee class. "HASA" relationship is best exhibited by containment or aggregation.

Explain Register Storage Specifier.

"Register" variable should be used whenever the variable is used. When a variable is declared with a "register" specifier, then the compiler gives CPU register for its storage to speed up the lookup of the variable.

State the difference between delete and delete[].

"delete[]" is used to release the memory allocated to an array which was allocated using new[]. "delete" is used to release one chunk of memory which was allocated using new.

Give an example of Run-time Polymorphism/Virtual Functions.

1class SHAPE{ 2 public virtual Draw() = 0; //abstract class with a pure virtual method 3 }; 4 class CIRCLE: public SHAPE{ 5 public int r; 6 public Draw() { this->drawCircle(0,0,r); } 7 }; 8class SQUARE: public SHAPE{ 9 public int a; 10 public Draw() { this->drawSquare(0,0,a,a); } 11 }; 12 13 int main() 14 { 15 SHAPE shape1*; 16 SHAPE shape2*; 17 18 CIRCLE c1; 19 SQUARE s1; 20 21 shape1 = &c1; 22 shape2 = &s1; 23 cout<<shape1->Draw(0,0,2); 24 cout<<shape2->Draw(0,0,10,10); 25 } In the above code, SHAPE class has a pure virtual function and is an abstract class (cannot be instantiated). Each class is derived from SHAPE implementing Draw () function in its own way. Further, each Draw function is virtual so that when we use a base class (SHAPE) pointer each time with the object of the derived classes (Circle and SQUARE), then appropriate Draw functions are called.

What do you mean by Pure Virtual Functions?

A Pure Virtual Member Function is a member function in which the base class forces the derived classes to override. Normally this member function has no implementation. Pure virtual functions are equated to zero. Example: class Shape { public: virtual void draw() = 0; }; Base class that has a pure virtual function as its member can be termed as an "Abstract class". This class cannot be instantiated and it usually acts as a blueprint that has several sub-classes with further implementation.

What is a Standard Template Library (STL)? What are the various types of STL Containers?

A Standard Template Library (STL) is a library of container templates approved by the ANSI committee for inclusion in the standard C++ specification. We have various types of STL containers depending on how they store the elements. Queue, Stack - These are the same as traditional queue and stack and are called adaptive containers. Set, Map - These are basically containers that have key/value pairs and are associative in nature. Vector, deque - These are sequential in nature and have similarity to arrays.

What is the difference between a Copy Constructor and an Overloaded Assignment Operator?

A copy constructor and an overloaded assignment operator basically serve the same purpose i.e. assigning the content of one object to another. But still, there is a difference between the two. Example: complex c1,c2; c1=c2; //this is assignment complex c3=c2; //copy constructor In the above example, the second statement c1 = c2 is an overloaded assignment statement. Here, both c1 and c2 are already existing objects and the contents of c2 are assigned to the object c1. Hence, for overloaded assignment statement both the objects need to be created already. Next statement, complex c3 = c2 is an example of the copy constructor. Here, the contents of c2 are assigned to a new object c3, which means the copy constructor creates a new object every time when it executes.

What is a COPY CONSTRUCTOR and when is it called?

A copy constructor is a constructor that accepts an object of the same class as its parameter and copies its data members to the object on the left part of the assignment. It is useful when we need to construct a new object of the same class.

What is a Reference Variable in C++?

A reference variable is an alias name for the existing variable. This means that both the variable name and the reference variable point to the same memory location. Hence, whenever the variable is updated, the reference is updated too. 1int a=10; int&b = a; Here, b is the reference of a.

Explain the Static Member Function.

A static member function can access only the static member variable of the class. Same as the static member variables, a static member function can also be accessed using the class name.

What is a Static Variable?

A static variable is a local variable that retains its value across the function calls. Static variables are declared using the keyword "static". Numeric variables which are static have the default value as zero. The following function will print 1 2 3 if called thrice. void f() { static int i; ++i; printf("%d ",i); } If a global variable is static, then its visibility is limited to the same source code.

What are Virtual Functions?

A virtual function allows the derived classes to replace the implementation provided by the base class. Whenever we have functions with the same name in the base as well as derived class, there arises an ambiguity when we try to access the child class object using a base class pointer. As we are using a base class pointer, the function that is called is the base class function with the same name. To correct this ambiguity we use the keyword "virtual" before the function prototype in the base class. In other words, we make this polymorphic function Virtual. By using a Virtual function, we can remove the ambiguity and we can access all the child class functions correctly using a base class pointer.

What is the difference between an External Iterator and an Internal Iterator? Describe an advantage of the 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 bound to the object that has items to step through. The basic advantage of an External iterator is that it's easy to implement as it is implemented as a separate class. Secondly, as it's a different class, many iterator objects can be active simultaneously.

Explain Function Overloading and Operator Overloading.

C++ supports OOPs concept Polymorphism which means "many forms". In C++ we have two types of polymorphism, i.e. Compile-time polymorphism, and Run-time polymorphism. Compile time polymorphism is achieved by using an Overloading technique. Overloading simply means giving additional meaning to an entity by keeping its base meaning intact. C++ supports two types of overloading: Function Overloading: Function overloading is a technique which allows the programmer to have more than one function with the same name but different parameter list. In other words, we overload the function with different arguments i.e. be it the type of arguments, number of arguments or the order of arguments. Function overloading is never achieved on its return type. Operator Overloading: This is yet another type of compile-time polymorphism that is supported by C++. In operator overloading, an operator is overloaded, so that it can operate on the user-defined types as well with the operands of the standard data type. But while doing this, the standard definition of that operator is kept intact. For Example, Addition operator (+) that operates on numerical data types can be overloaded to operate on two objects just like an object of complex number class.

What are the various Access Specifiers in C++?

C++ supports the following access specifiers: Public: Data members and functions are accessible outside the class. Private: Data members and functions are not accessible outside the class. The exception is the usage of a friend class. Protected: Data members and functions are accessible only to the derived classes. Example: Describe PRIVATE, PROTECTED and PUBLIC along with their differences and give examples.

What is the difference between an Object and a Class?

Class is a blueprint of a project or problem to be solved and consists of variables and methods. These are called the members of the class. We cannot access methods or variables of the class on its own unless they are declared static. In order to access the class members and put them to use, we should create an instance of a class which is called an Object. The class has an unlimited lifetime whereas an object has a limited lifespan only.

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 details of the working of a class. In general, class acts as a blueprint of a project and can include in various parameters and functions or actions operating on these parameters. These are called the members of the class.

What is a Constructor and how is it called?

Constructor is a member function of the class having the same name as the class. It is mainly used for initializing the members of the class. By default constructors are public. There are two ways in which the constructors are called: Implicitly: Constructors are implicitly called by the compiler when an object of the class is created. This creates an object on a Stack. Explicit Calling: When the object of a class is created using new, constructors are called explicitly. This usually creates an object on a Heap.

What is a Default Constructor?

Default constructor is a constructor that either has no arguments or if there are any, then all of them are default arguments class B { public: B (int m = 0) : n (m) {} int n; };

Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.

False. Both, Passing by value and Passing by reference look identical to the caller.

What is a friend class?

Friend classes are used when we need to override the rule for private and protected access specifiers so that two classes can work closely with each other. Hence, we can have a friend class to be the friend of another class. This way, friend classes can keep private, inaccessible things in the way they are. When we have a requirement to access the internal implementation of a class (private member) without exposing the details by making the public, we go for friend functions.

What is an Iterator class?

In C++ a container class is a collection of different objects. If we need to traverse through this collection of objects, we cannot do it using simple index variables. Hence, we have a special class in STL called an Iterator class which can be used to step through the contents of the container class. The various categories of iterators include input iterators, output iterators, forward iterators, bidirectional Iterators, random access etc.

What are Multiple Inheritances (virtual inheritance)? What are its advantages and disadvantages?

In multiple inheritances, we have more than one base classes from which a derived class can inherit. Hence, a derived class takes the features and properties of more than one base class. For Example, a class driver will have two base classes namely, employee and a person because a driver is an employee as well as a person. This is advantageous because the driver class can inherit the properties of the employee as well as the person class. But in the case of an employee and a person, the class will have some properties in common. However, an ambiguous situation will arise as the driver class will not know the classes from which the common properties should be inherited. This is the major disadvantage of multiple inheritance.

Q #25) What is wrong with this code? T *p = 0; delete p;

In the above code, the pointer is a null pointer. Hence naturally, the program will crash in an attempt to delete the null pointer.

What are the advantages of Inheritance?

Inheritance allows code re-usability, thereby saving time on code development. By inheriting, we make use of a bug-free high-quality software that reduces future problems.

What is Inheritance?

Inheritance is a process by which we can acquire the characteristics of an existing entity and form a new entity by adding more features to it. In terms of C++, inheritance is creating a new class by deriving it from an existing class so that this new class has the properties of its parent class as well as its own.

What is an Inline function in C++?

Inline function is a function that is compiled by the compiler as the point of calling the function and the code is substituted at that point. This makes compiling faster. This function is defined by prefixing the function prototype with the keyword "inline". Such functions are advantageous only when the code of the inline function is small and simple. Although a function is defined as Inline, it is completely compiler dependent to evaluate it as inline or not.

What is a Conversion Constructor?

It is a constructor that accepts one argument of a different type. Conversion constructors are mainly used for converting from one type to another.

What is the difference between Method Overloading and Method Overriding in C++?

Method overloading is having functions with the same name but different argument list. This is a form of compile-time polymorphism. Method overriding comes into picture when we rewrite the method that is derived from a base class. Method overriding is used while dealing with run-time polymorphism or virtual functions.

What is Namespace?

Namespaces allow us to group a set of global classes, objects and/or functions under a specific name. The general form to use namespaces is: namespace identifier { namespace-body } Where identifier is any valid identifier and the namespace-body is the set of classes, objects, and functions that are included within the namespace. Namespaces are especially useful in the case where there is a possibility for more than one object to have the same name, resulting in name clashes.

What is the role of Static keyword for a class member variable?

Static member variable shares a common memory across all the objects created for the respective class. We need not refer to the static member variable using an object. However, it can be accessed using the class name itself.

What is a Storage Class? Mention the Storage Classes in C++.

Storage class determines the life or scope of symbols such as variable or functions. C++ supports the following storage classes: Auto Static Extern Register Mutable

Difference between Class and Structure.

Structure: In C language, the structure is used to bundle different type of data types together. The variables inside a structure are called the members of the structure. These members are by default public and can be accessed by using the structure name followed by a dot operator and then the member name. Class: Class is a successor of the Structure. C++ extends the structure definition to include the functions that operate on its members. By default all the members inside the class are private.

What is a template?

Templates allow creating functions that are independent of data type (generic) and can take any data type as parameters and return value without having to overload the function with all the possible data types. Templates nearly fulfill the functionality of a macro. Its prototype is any of the following ones: template <class identifier> function_declaration; template <typename identifier> function_declaration; The only difference between both the prototypes is the use of keyword class or typename. Their basic functionality of being generic remains the same.

What is wrong with this code? T *p = new T[10]; delete p;

The above code is syntactically correct and will compile fine. The only problem is that it will just delete the first element of the array. Though the entire array is deleted, only the destructor of the first element will be called and the memory for the first element is released.

What is Polymorphism?

The basic idea behind polymorphism is in many forms. In C++, we have two types of Polymorphism: (i) Compile time Polymorphism In compile time polymorphism, we achieve many forms by overloading. Hence, we have Operator overloading and function overloading. (We have already covered this above) (ii) Run-time Polymorphism This is the polymorphism for classes and objects. General idea is that a base class can be inherited by several classes. A base class pointer can point to its child class and a base class array can store different child class objects. This means, that an object reacts differently to the same function call. This type of polymorphism can use virtual function mechanism.

Explain Mutable Storage class specifier.

The variable of a constant class object's member cannot be changed. However, by declaring the variables as "mutable", we can change the values of these variables.

When to use "const" reference arguments in a function?

Using "const" reference arguments in a function is beneficial in several ways: "const" protects from programming errors that could alter data. As a result of using "const", the function is able to process both const and non-const actual arguments, which is not possible when "const" is not used. Using a const reference, allows the function to generate and use a temporary variable in an appropriate manner.

What is the use of 'using' declaration?

Using declaration is used to refer a name from the namespace without the scope resolution operator.

What are Virtual Constructors/Destructors?

Virtual Destructors: When we use a base class pointer pointing to a derived class object and use it to destroy it, then instead of calling the derived class destructor, the base class destructor is called. Virtual constructor: Constructors cannot be virtual. Declaring a constructor as a virtual function is a syntax error.

Does a derived class inherit or doesn't inherit?

When a derived class is constructed from a particular base class, it basically inherits all the features and ordinary members of the base class. But there are some exceptions to this rule. For instance, a derived class does not inherit the base class's constructors and destructors. Each class has its own constructors and destructors. The derived class also does not inherit the assignment operator of the base class and friends of the class. The reason is that these entities are specific to a particular class and if another class is derived or if it is the friend of that class, then they cannot be passed onto them.

What is Exception Handling? Does C++ support Exception Handling?

Yes C++ supports exception handling. We cannot ensure that code will execute normally at all times. There can be certain situations which might force the code written by us to malfunction, even though it's error-free. This malfunctioning of code is called Exception. When an exception has occurred, the compiler has to throw it so that we know an exception has occurred. When an exception has been thrown, the compiler has to ensure that it is handled properly, so that the program flow continues or terminates properly. This is called handling of an exception. Thus in C++, we have three keywords i.e. try, throw and catch which are in exception handling. The general syntax for exception block is: try{ .... # Code that is potentially about to throw exception goes here .... throw exception; } catch(exception type) { ... #code to handle exception goes here } As shown above, the code that might potentially malfunction is put under the try block. When code malfunctions, an exception is thrown. This exception is then caught under the catch block and is handled i.e. appropriate action is taken.

Name the Operators that cannot be Overloaded.

sizeof - sizeof operator . - Dot operator .* - dereferencing operator -> - member dereferencing operator :: - scope resolution operator ?: - conditional operator


Set pelajaran terkait

FINA 470- Chapters 8-11: Multiple Choice

View Set

Geography 2750 Spring 2019 Midterm 1 Review

View Set

CompTIA Network+ Chapters 6 and 7

View Set

Fundamentals Nursing Prep U Chapter 16 Documenting, Reporting, Conferring, and Using Informatics

View Set

LC10: LearningCurve - Ch. 10: Externalities and Public Goods

View Set

CCNA Guide to Cisco Networking - Chapter 1 Review Questions

View Set