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

C++ Programming Quiz 9 In all questions involving function headers, be sure to t

ID: 3707118 • Letter: C

Question

C++ Programming

Quiz 9

In all questions involving function headers, be sure to think of whether function arguments and return values should be references, whether they should be const, etc. (Remember that a function header is the line at the beginning when the function is implemented.)

Write a template function to swap two values.

Class Stuff is a template class that only has one parameter. Write the standalone function header for the overloaded assignment operator for the class.

Given that List is a template class, write the code expression which declares mylist, a list of integers.

Give an example of a "last in, first out" data structure.

Give an example of a "first in, first out" data structure.

Explanation / Answer

EXPLANATION:

1.what is the keyword this?

ans:this is the keyword and acts as a pointe.it holds the address of an object which invokes the memberfunction of class members.

usage:this->n=n;

to understand this read the below code:

#include<iostream.h>

#include<conio.h>

class number

{

private:

int n;

public:

void assign(int n)

{ /here n is the argument passed by the function caller.and we have n as the private data member.to distinguish both of them we are using the this pointer with the class datamember./

this->n=n;

/if we write the above statement like n=n;it generate ambiguity in the program/

}

void print()

{

cout<<"n is "<<n;

}};

void main()

{number n;

n.assign(10);

n.print();

getch();

}

2.what is the destructor?

ans: destructor is a member function of a class which will executes automatically at time of the object scope ends.the destructor job is to destroy or deallocates the heap space which is allocated by the constructor for the class object.

3.write the method headerfor the destructor for class Widget?

ans: the method header or declaration of the destructor for Widget class can be written as

class Widget

{

public:

~widget()

{

//body of the destructor

}

};

4.Write the method header for the copy constructor for class widget?

ans: the copy constructor is the constructor which is used to initialize the object with the same class object.the declaration of the copy construtor can written as :

Widget(const Widget &obj)

{

//body of the constructor

}

5.Write the method header for the overloaded assignment operator for class Widget?

ans: Method Header for Widget class is as follows:

friend Widget operator =(Widget,Widget);

here,friend and operator are the keywords.to overload an operator we need to define the friend function.

6.what are two situations where a copy constructor may be called:

ans:we can use the copy constructor:

a.when we pass a class object as the parameter for the same class.

b.when object of the class is constructed by using another object of the same class.

7.what is the logic for an overloaded assignment operator?

ans:to write th logic for the function we need to know the datamembers of the class.here i am writing the program to understand the logic.

PROGRAM CODING:

#include< iostream.h>

#include<conio.h>

class Example

{

private:

int a,b;

public:

void read()

{

cout<<"Enter two number:";

cin>>a>>b;

}

void print()

{

cout<<"a="<<a<<" "<<"b="<<b<<" ";

}

friend number operator =(number);

};

number operator =(const number x)

{

number temp;

temp.a=x.a;

temp.b=x.b;

return temp;

}

void main()

{

number a,b;

clrscr();

a.read();

b=a; // here we are using the assignment operator with the objects of the Number class as simple as variables

//this is how we can overload a operator

b.print();

getch();

}

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