Write the function header of the default copy constructor


Q1. Consider the following program segment.

1 class A {

2 private:

3 int a = 1, b, c, d, *ptr;

4 public:

5 A() { int pr = new int[10];)

6 A (int b = 0) : this -> b(b) {}

7 void seta(int x) const {a = x;}

8 void setB(int x) {this.b = x ;}

9 void setC() {this -> C = ptr {0} ; }

10 void setD(int x) {d = x;}

11 -A(int *A) {

12 delete ptr;

13 ptr = x;

14 }

15

16 void main () {

17 const A a1;

18 A a2(1, 2, 3);

19 a1.setD(6);

20 cout << a1.ptr[1] << end 1;

1.a What's wrong with lint 3?

1.b What's wrong with the constructors?

1.c What's wrong with member function setA()?

1.d What's wrong with the function setB()?

1.e What's wrong with the function setC()?

1.f What's wrong with the destructor?

1.g What's wrong with the class itself?

1.h What's wrong with line 18?

1.i What's wrong with line 19?

1.j What's wrong with line 20?

Q2. State True or False for each of the following statements about friend function

i. Friend function is a member function.

ii. Friend function is declared, not defined, in a class.

iii. Friend function has the right to access the public and private members of the class in which it is declared.

iv. Friend function is inherited.

v. Friend function of a class can be a member function of another class.

Q3. Write the function header of the default copy constructor for a class called A.

Q4. Write one default constructor prototype of a class called A.

Q5. Write a destructor of class B that deletes the object pointed by the pointer ptr.

Q6. Write the class headier of a class called B that use protected inheritance to inherit class J.

Q7. Write a statement that dynamically mate object of class B;

Q8. Fill in the blanks.

a. _________ is a form of software reuse in which new classes absorb the data and behaviors of existing classes and embellish these classes with new capabilities.

b. A function that is not defined in a class is called _________.

c. C++ provides for _________, which allows a derived class to inherit from many base classes, even if the base classes are unrelated.

d. When an object of a derived class is instantiated, the base class's _________ is called implicitly or explicitly to do any necessary initialization of the base class data members in the derived class object.

e. When a variable is declared in {}, it has a _________ scope.

f. A variable that maintains its value when the program execution exist the block where the variable was declared is a _________        variable.

g. The capability of a nonmember function having full access to a class's members is called a _________ of the class.

h. Friend function is _________, not inherited.

i. Inheritance is an _________ relationship between the base and derived classes.

j. Member functions of a derived class can access the _________ and _________ members of the base class.

k. Data members are commonly declared as private, called _________, which is good software engineering.

l. Public member functions provide the _________ with the outside world through their parameter lists and the returned data.

m. Dynamically created object (using the new operator) can only be destroy by using the _________ operator.

Q9. A derived class represents a _________ with respect to its base class

i. more generalized object

ii. more specialized object

iii. less generalized object

iv. less specialized object

Q10. Which two of the following statements about inheritance are true?

i. An object of the base class is also an object of its derived class.

ii. An Object of the base class is not an object of it derived class.

iii. An object of the derived class is also an object of its base class.

iv. An object of the derived class is not an object of its base class.

Q11. Which one of the following statements about inheritance is false?

i. When a nonmember function is passed an object of a derived class, the nonmember function cannot access any of the base class's members if the inheritance is either protected or private.

ii. A private member function of a class cannot access the public members of the class.

iii. When a nonmember function is passed an object of a derived class, the nonmember function can access any of the base class's members via an object of the derived class, if the inheritance is public.

iv. A protected member function of a class can access all members of the class.

Q12. Which one of the following statements about inheritance is false?

i. A friend function of a derived class can access the public members of the based class.

ii. A friend function of a derived class cannot access the protected members of the based class.

iii. A member function of a derived class cannot access the private member of the based class.

iv. A member function of a derived class cannot access the private member of the based class.

Q13. A derived class can inherit from a _________ base class.

i. public, protected, or private

ii. public and protected only

iii. pubic only

iv. protected only

All the remaining questions are points each.

Q14. Consider the following program segment:

1 class A {

2 private :

3 B * bObjPtr ; //assume class B is defined with a default constructor

4 public :

5 A() {

6 bObjPtr = new B;

7 }

8 B get_BObj (){

9 return *bObjPtr;

10 }

11 }

Write a destructor for class A to destroy the B object.

Q15. Consider the following program segment:

1 class A {

2 private :

3 B *bObjPtr ; //assume class B is defined with a default constructor

4 public

5 A () {

6 bObjPtr = new B ;

7 }

8 B get_BObj () {

9 return *bObjPtr ;

10 }

11 } ;  

Write a copy constructor.

Q16. Refer 'to Question 15. Write the copy constructor that copies the dynamically created object of class B.

Q17. Write a declaration statement that does not allow a pointer, of type, double, to be modified once it is initialized and also does not allow the data pointed by the pointer to be modified.

Q18. Consider the following function:

1 void myfunction () {

2 Time *timePtr = new Time (12,  0,  30);

3 cout << "A new Time object is just created\n";

4 }

Anything wrong with this function'? Explain.

Q19. Modify the following function definition to support return-by-reference using pointer.

1 Time getTime () {

2 Time *myttime = new Time (3,  0, 0);

3 return *mytime;

4 }

Q20. Consider the following program segment:

1 double add (double x, double y)

2 {

3 return x + y ;

4 }

Write a function template for the above function.

Q21. Explain how the constructor of the base class is executed when an object of its derived class is instantiated?

Attachment:- Assignment Files.zip

Request for Solution File

Ask an Expert for Answer!!
Programming Languages: Write the function header of the default copy constructor
Reference No:- TGS02381285

Expected delivery within 24 Hours