DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI Department of Computer Science and Engineering CS6301 PROGRAMMING AND DATA STRUCTURES-II Anna University 2 & 16 Mark Questions & Answers Year / Semester: II / III Regulation: 2013 Academic year: 2017 - 2018
33
Embed
DHANALAKSHMI COLLEGE OF ENGINEERING, … COLLEGE OF ENGINEERING, CHENNAI ... Anna University 2 & 16 Mark Questions & Answers ... C obj; obj.display();
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
DHANALAKSHMI COLLEGE OF ENGINEERING,
CHENNAI
Department of Computer Science and Engineering
CS6301 PROGRAMMING AND DATA STRUCTURES-II
Anna University 2 & 16 Mark Questions & Answers
Year / Semester: II / III
Regulation: 2013
Academic year: 2017 - 2018
PART A
1. How does a C string differs from a C++ type string? [M/J – 16] [N/D-15]
In C, strings are just char arrays which, by convention, end with a NUL byte. In terms of
dynamic memory management, you can simply malloc the space for them (including the extra
byte). Memory management when modifying strings is your responsibility:
char *s = strdup ("Hello");
char *s2 = malloc (strlen (s) + 6);
strcpy (s2, s);
strcat (s2, ", Pax");
free (s);
s = s2;
In C++, strings (std::string) are objects with all the associated automated memory management
and control which makes them a lot safer and easier to use, especially for the novice. For
dynamic allocation, use something like:
std::string s = "Hello";
s += ", Pax";
2. Distinguish the term overloading and overriding. [M/J – 16]
Overloading occurs when two or more methods in one class have the same method name but
different parameters. Overriding means having two methods with the same method name and
parameters (i.e., method signature). One of the methods is in the parent class and the other is in
the child class.
3. Give the list of operators that cannot be overloaded.
?: (conditional)
. ( member selection)
.* (member selection with pointer-to-member)
:: (scope resolution)
sizeof (object size information)
typeid (object type information)
4. Differentiate compile and runtime polymorphism.
Compile time Polymorphism, call is resolved by
the compiler.
In Run time Polymorphism, call is not resolved by
the compiler.
It is also known as Static binding, Early
bindingand overloading as well.
It is also known as Dynamic binding, Late
bindingand overriding as well.
Overloading is compile time polymorphism where Overriding is run time polymorphism having
more than one methods share the same name with
different parameters or signature and different
return type.
same method with same parameters or signature,
but associated in a class & its subclass.
It is achieved by function overloading
andoperator overloading.
It is achieved by virtual functions and pointers.
It provides fast execution because known early at
compile time.
It provides slow execution as compare to early
binding because it is known at runtime.
Compile time polymorphism is less flexible as all
things execute at compile time.
Run time polymorphism is more flexible as all
things execute at run time.
5. Define copy constructor and its use.
The copy constructor is a constructor which creates an object by initializing it with an object
of the same class, which has been created previously. The copy constructor is used to: Initialize
one object from another of the same type. Copy an object to pass it as an argument to a function.
6. What are the special properties of virtual functions?
Dynamic binding: virtual functions are resolved during run time or dynamic binding.
Virtual functions are member functions of a class.
Virtual functions are declared with the keyword virtual.
Virtual function takes a different functionality in the derived class.
8. What is dynamic initialization of objects?
Dynamic initialization of object refers to initializing the objects at run time i.e. the initial value
of an object is to be provided during run time. Dynamic initialization can be achieved using
constructors and passing parameters values to the constructors. This type of initialization is
required to initialize the class variables during run time.
10. What are pure virtual functions? Give example.
A pure virtual function or pure virtual method is a virtual function that is required to be
implemented by a derived class if the derived class is not abstract. Classes containing pure
virtual methods are termed "abstract" and they cannot be instantiated directly.
class Animal {
public:
void /*non-virtual*/ move(void) {
std::cout << "This animal moves in some way" << std::endl;
}
virtual void eat(void) = 0;
};
// The class "Animal" may possess a definition for eat() if desired.
class Llama : public Animal {
public:
// The non virtual function move() is inherited but not overridden
void eat(void) override {
std::cout << "Llamas eat grass!" << std::endl;
}
};
Part B
12. (a) (i) Write a C++ program to overload the decrement operator with prefix and postfix
forms.
#include<iostream>
using namespace std;
class pp
{
int a,b;
public:
pp()
{
a=5;
b=-5;
}
void show()
{
cout<<" a="<<a<<" and b="<<b;
}
void operator --()
{
--a;
--b;
}
void operator --(int)
{
a--;
b--;
}
};
int main()
{
pp p,p1;
cout<<"Before prefix decrement";
p.show();
--p;
cout<<"\nAfter prefix decrement";
p.show();
cout<<"\nBefore postfix decrement";
p1.show();
p1--;
cout<<"\nAfter postfix decrement";
p1.show();
}
(ii) Explain any two types of inheritance supported in C++ with suitable examples.
C++ Multilevel Inheritance
In C++ programming, not only you can derive a class from the base class but you can also derive
a class from the derived class. This form of inheritance is known as multilevel inheritance.
class A
{
... .. ...
};
class B: public A
{
... .. ...
};
class C: public B
{
... ... ...
};
Here, class B is derived from the base class A and the class C is derived from the derived
class B.
Example 1: C++ Multilevel Inheritance
#include <iostream>
using namespace std;
class A
{
public:
void display()
{
cout<<"Base class content.";
}
};
class B : public A
{
};
class C : public B
{
};
int main()
{
C obj;
obj.display();
return 0;
}
Output
Base class content.
In this program, class C is derived from class B (which is derived from base class A).
The obj object of class C is defined in the main() function.
When the display() function is called, display() in class A is executed. It's because there is
no display() function in class C and class B.
The compiler first looks for the display() function in class C. Since the function doesn't exist
there, it looks for the function in class B (as C is derived from B).
The function also doesn't exist in class B, so the compiler looks for it in classA (as B is derived
from A).
If display() function exists in C, the compiler overrides display() of class A(because of member
function overriding).
C++ Multiple Inheritance
In C++ programming, a class can be derived from more than one parents. For example: A
class Bat is derived from base classes Mammal and WingedAnimal. It makes sense because bat
is a mammal as well as a winged animal.
Example 2: Multiple Inheritance in C++ Programming
This program calculates the area and perimeter of an rectangle but, to perform this program,