Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ short questions. if you can provide an exmaple to understand its better Prov

ID: 662900 • Letter: C

Question

C++ short questions. if you can provide an exmaple to understand its better

Provide short answers for the following questions. Feel free to add short examples if they help you to explain the concepts involved. (3 mark each) (a) What effect does marking a method as const have? (b) Explain the purpose of the initialisation list (colon section) used in constructors in relation to inherited data members. (c) Explain why you need to provide a copy constructor and overload the assignment operator for classes where a deep copy needs to be implemented (e.g. in the case of a class containing data members that are dynamically allocated at runtime). (d) Explain how C++ supports encapsulation.

Explanation / Answer

Answer:

(a) Answer: Defining a super class method that is prefixed with const key word. That method cannot be overrided by derived class. During compilation error is raised.

Eg: class A

{

public:

const void show()

{

//any set of statements here

}

};

class B:public A

{

public:

void show() //trying to override base class A method, but it is error

{

///any code here

}

};

(b) Answer: Using ::(scope resolution operator) in constructor for super class data members inititalization is required. If Super class and sub class defines same data members. Two initialize both of data members use :: operator.

Eg:

class A

{

protected:

int a;

public:

A()

{

///code here

}

};

class B:public A

{

protected:

int a;

public:

B(int ta1,int ta2)

{

A::a=ta1;

a=ta2;

}

void show()

{

cout<<" A from Super class : "<<A::a;

cout<<" A from sub class : "<<a;

}

};

(c) Answer: Copy constructor is used when new object is created as cloned to already existing object.

Eg: Box b1(90,2,4); //Box b1 created with 03 parameters height,width and depth

Box b2(b1); //Now b1 properties are copied to b2


- If overloading of assignment operator '=' is required when dynamic memory allocation is done using pointer and new operator for object creation.

Eg: Box b1(90,2,4); //Box b1 created with 03 parameters height,width and depth

Box *b2;

b2=new Box(b1); //b2 is a dynamic Box object run-time properties are allocated


(d) Answer: Encapsulation is making things together. C++ class is combined together with <data members> and <member functions>.

These data members and member functions are having access specifiers

*private: Private members are visible only inside the class

*protected:Protected members are visible inside the class and outside sub classes only

*public: Public members are visible from any part of program

Eg:

class Box

{

private:

int height; //this is accessible only inside Box class

protected:

int width;//this is accessible inside Box class and sub classes of Box

public:

int depth;//this is accessible from any part of program

};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote