Questions 4. Consider the definition of the following class: class CC { public :
ID: 3626696 • Letter: Q
Question
Questions 4. Consider the definition of the following class:class CC
{
public :
CC ();
CC (int);
CC (int, int);
CC ( double, int);
.
.
.
private :
int u;
double v;
};
a. Give the line number containing the constructor that is executed in each of the following declarations.
i. CC one;
ii. CC two (5,6);
iii. CC three (3.5, 8);
b. Write the definition of the constructor in Line 1 so that the private member variables are initialized to 0.
c. Write the definition of the constructor in Line 2 so that the private member variable u is initialized according to the value of the parameter, and the private member variable v is initialized to 0.
d. Write the definition of the constructors in Lines 3 and 4 so that the private member variables are initialized according to the values of the parameters.
6. Given the definition of the class clockType with constructors (as described in this chapter), what is the output of the following C++ code?
clockType clock1;
clockType clock2 (23,13,75);
clock1.printTime();
cout<<endl;
clock2.printTime();
cout<<endl;
clock1.setTime (6, 59, 39);
clock1.printTime();
cout<<endl;
clock1.incrementMinutes ();
clock1.printTime();
cout<<endl;
clock1.setTime (0, 13, 0);
if (clock1.equalTime (clock2))
cout << "clock1 time is the same as clock2 time."
<< endl;
else
cout << "The two times are different." << endl;
8. Explain why you would need both public and private members in a class.
10. Which of the following characters appears before a destructor's name?
a. #
b. !
c. ~
d. $
12. Write the definition of a class that has the following properties:
a. The name of the class in secretType.
b. The class secretType has four member variables: name of type string, age and weight of type int, and height of type double.
c. the class secretType has the following member functions. (Make each accessor function constant.)
print- outputs the data stored in the member variables with the appropriate titles.
setName- function to set the name
setAge- function to set the age
setWeight- function to set the weight
setHeight- function to set the height
getName- value-returning function to return the name
getAge- value-returning function to return the age
getWeight- value-returning function to return the weight
getHeight- value-returning function to return the height
constructor- with default parameters: The default value of name is the empty string " ", and the default values of age, weight, and height, are 0.
d. Write the definition of the member functions of the class secretType, as describe in part c.
Please help me with all 5 questions. need ASAP. thank you.
Explanation / Answer
Questions 4.
Consider the definition of the following class:
class CC
{
public :
CC (); //line 1
CC (int); //line 2
CC (int, int); //line 3
CC (double, int); //line 4
.
.
.
private:
int u;
double v;
};
Give the line number containing the constructor that is executed in each of the following declarations.
i. CC one;
ii. CC two (5,6);
iii. CC three (3.5, 8);
Answer:
i. line 1
ii. line 3
iii. line 4
Explanation:
CC two (5,6); It is parameterized (argumentary) constructor which takes 2 integer type arguments.
Write the definition of the constructor in Line 1 so that the private member variables are initialized to 0.
Write the definition of the constructor in Line 2 so that the private member variable u is initialized according to the value of the parameter, and the private member variable v is initialized to 0.
CC(int a)
Write the definition of the constructors in Lines 3 and 4 so that the private member variables are initialized according to the values of the parameters.
Lines 3
CC(int a,int b)
Lines 4
CC(double a , int b)
6.
Answer:
The output for the given code would be is as shown below:
00:00:00
23:13:00
06:59:39
07:00:39
The two times are different.
8.
Explain why you would need both public and private members in a class.
Public members:
Private members:
It is needed both public and private members in a class because by default all the members of a class are private and the methods are declared as public in class as the method will be called later.
The class consists of data members and member functions. The class will be useless to define all the members of a class as private since nobody can access private members of a class. Therefore to define the class members as public depends on the requirement. The public members of the class can be accessed by other classes/programs.
10.
Which of the following characters appears before a destructor's name?
a. #
b. !
c. ~
d. $
A destructor is a special member function of a class that is executed whenever an object of its class goes out of scope. A destructor will have an exact same name as the class prefixed with a tilde (~).
Hence, the correct option is c.
12.
class secretType
{
string name;
int age;
int weight;
double height;
public:
void print() const
{
cout << "name " << name << " age " << age
<< " weight " << weight << " height " << height
<< endl;
}
void setName(string N)
{
name = N;
}
void setAge(int N)
{
age = N;
}
void setWeight(int N)
{
weight = N;
}
void setHeight(double N)
{
height = N;
}
string getName() const
{
return name;
}
int getAge() const
{
return age;
}
int getWeight() const
{
return weight;
}
double getHeight()const
{
return height;
}
secretType(string a = " ", int b = 0, int c = 0,
double d = 0)
{
name = a;
age = b;
weight = c;
height = d;
}
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.