California Baptist University, Online & Professional Studies
California Baptist University, Online & Professional Studies
Week 3 Homework
California Baptist University, Online & Professional Studies
Please typeset your answers to the following homework problems (you may save this file as
week3hwk_your_lastname.doc and then directly type your answers after each problem). Submit
this assignment on Blackboard after you have finished by the due time.
int main()
{
Entry entry1("John", "Walker", "McAlpine");
Entry entry2("Mary", "Beth", "Smith", 10, 30);
entry1.printTime();
cout << " " << entry1.getNameStr() << endl;
entry2.printTime();
cout << " " << entry2.getNameStr() << endl;
return 0;
}
3. Consider the Entry class shown in the Week 3 Day 1 Example Programs for Composition and
Polymorphism (which contains a class Name object name and a class Time object time as
private members). If we now want to add one more member function to overload the operator
== for the Entry class, which has the declaration as
bool operator==(const Entry& otherEntry) const;
write an implementation of this operator overload function.
4. What is the output of the following C++ program?
#include <iostream>
using namespace std;
class First
{
protected:
int a;
public:
First(int x = 1)
{ a = x; }
int getVal()
{ return a; }
};
1
private:
int b;
public:
Second(int y = 5)
{ b = y; }
int getVal()
{ return b; }
};
int main()
{
First object1;
Second object2;
class First
{
protected:
int a;
public:
First(int x = 1)
{ a = x; }
void twist()
{ a *= 2; }
int getVal()
{ twist(); return a; }
};
void twist()
{ b *= 10; }
};
int main()
{
First object1;
Second object2;
2
using namespace std;
class First
{
protected:
int a;
public:
First(int x = 1)
{ a = x; }
int getVal()
{ twist(); return a; }
};
int main()
{
First object1;
Second object2;
class BaseClass
{
public:
BaseClass(int a = 1, int b = 2)
{ x = a; y = b; }
virtual void func1() const
{ cout << "x = " << x << " in BaseClass" << endl; }
void func2() const
{ cout << "y = " << y << " in BaseClass" << endl; }
protected:
int x;
int y;
};
3
void func1() const
{ cout << "x = " << x << " in DerivedClass" << endl; }
void func2() const
{ cout << "y = " << y << " in DerivedClass" << endl; }
};
int main()
{
BaseClass baseObject;
DerivedClass derivedObject(7, 8);
print(baseObject);
print(derivedObject);
return 0;
}
void print(BaseClass& s)
{
s.func1();
s.func2();
}
4
(f)
*p = q;
12. What is the output of the following C++ code?
int x;
int y;
int *p = &x;
int *q = &y;
*p = 35;
*q = 98;
*p = *q;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
13. What is the output of the following C++ code?
int x;
int y;
int *p = &x;
int *q = &y;
x = 35; y = 46;
p = q;
*p = 78;
cout << x << " " << y << endl;
cout << *p << " " << *q << endl;
14. Given the declaration
int num = 6;
int *p = #
which of the following statement(s) increment the value of sum?
(a)
p++;
(b)
(*p)++;
(c)
num++;
(d)
(*num)++;
15. Mark the following statements as true or false.
(a) In C++, pointer is a reserved word.
(b) In C++, pointer variables are declared using the reserved word pointer.
(c) The statement delete p; deallocates the variable pointer p.
(d) The statement delete p; deallocates the dynamic variable to which p points.
(e) Given the declaration
int list[10];
int *p;
5
the statement
p = list;
is valid in C++.
(f) Given the declaration
int *p;
the statement
p = new int[50];
dynamically allocates an array of 50 components of type int, and p contains the base
address of the array.
(g) The address of operator returns the address and value of its operand.
(h) If p is a pointer variable, the statement p = p * 2; is valid in C++.
16. What is the output of the following code?
int *p;
int * q;
p = new int;
q = p;
*p = 46;
*q = 39;
cout << *p << " " << *q << endl;
17. What is the output of the following code?
int *p;
int *q;
p = new int;
*p = 43;
q = p;
*q = 52;
p = new int;
*p = 78;
q = new int;
*q = *p;
cout << *p << " " << *q << endl;
18. What is wrong with the following code?
int *p; //Line 1
int *q; //Line 2
q = p; //Line 5
*q = 52; //Line 6
delete q; //Line 7
6
19. What is the output of the following code?
int x;
int *p;
int *q;
p = new int[10];
q = p;
*p = 4;
for (int j = 0; j < 9; j++)
{
x = *p;
p++;
*p = x + j;
}
for (int k = 0; k < 10; k++)
{
cout << *q << " ";
q++;
}
cout << endl;
20. Is the following program correct? If yes, what is the output of the program? If no, what is
wrong with the program?
#include <iostream>
using namespace std;
int main()
{
const int NO_OF_STUDENTS = 5;
double grade[NO_OF_STUDENTS] = {90, 90, 100, 80, 75.5};
double *ptr;
ptr = calcAvg(grade, NO_OF_STUDENTS);
cout << "The class average grade is: " << *ptr << endl;
delete ptr;
ptr = NULL;
return 0;
}
7
for (int j = 0; j < 10; j++)
cout << secret[j] << " ";
cout << endl;
22. What is wrong with the following code?
int *p; //Line 1
int *q; //Line 2
q = p; //Line 7
delete [] p; //Line 8
p = new int[5];
p[0] = 5;
q = new int[5];
8
for (int i = 0; i < 5; i++)
p[i] = new int[3];