• Home
  • Textbooks
  • Data Structures and Algorithms in C++
  • OBJECT-ORIENTED PROGRAMmING USING C++

Data Structures and Algorithms in C++

Adam Drozdek

Chapter 1

OBJECT-ORIENTED PROGRAMmING USING C++ - all with Video Answers

Educators


Chapter Questions

01:48

Problem 1

If $i$ is an integer and $p$ and $q$ are pointers to integers, which of the following assignments cause a compilation error?
(a) $\mathrm{p}=\& i$;
(e) $i=\star \& p$;
(i) $q=* * \& p$;
(b) $\mathrm{p}=\star \& i$;
(f) $i=\& \star p$;
(j) $q=* \& p$;
(c) $p=\& * i$
(g) $p=\& * \& i$;
(k) $q=\& \star p$;
(d) $i=* \& * p$;
(h) $q=\star \& \star p$;

Mayank Tripathi
Mayank Tripathi
Numerade Educator

Problem 2

Identify the errors; assume in (a) and (b) that $s 1$ has been declared and assigned a string:
(a) char* f( char *s) {
char ch = ' A ' ;
return &ch;
}
(b) $\operatorname{char}{ }^* \mathrm{~s} 1$;
$\operatorname{strcpy}(\mathrm{s} 1, \mathrm{~s} 2)$;
(c) $\operatorname{char} * \mathrm{~s} 1$;
$s 1=$ new $\operatorname{char}[\operatorname{strlen}(s 2)] ;$
$\operatorname{strcpy}(\mathrm{s} 1, \mathrm{~s} 2)$;

Check back soon!
03:26

Problem 3

Providing that the declarations
$$
\text { int intArray }[]=\{1,2,3\}, \star p=\text { intArray; }
$$
have been made, what will be the content of intArray and $p$ after executing
a. $* \mathrm{p}++$;
b. $\left({ }^{\star} p\right)++;$
c. ${ }^* \mathrm{p}++;(* \mathrm{p})++;$

Ernest Castorena
Ernest Castorena
Numerade Educator

Problem 4

Using only pointers (no array indexing), write
a. A function to add all numbers in an integer array,
b. A function to remove all odd numbers from an ordered array; the array should remain ordered. Would it be easier to write this function if the array were unordered?

Check back soon!

Problem 5

Using pointers only, implement the following string functions:
a. strlen()
b. strcmp()
c. strcat()
d. strchr()

Check back soon!
01:28

Problem 6

What is the difference between if $(\mathrm{p}==\mathrm{q})\{\ldots\}$ and if $(* \mathrm{p}==* \mathrm{q})\{\ldots\}$ ?

Ronald Prasad
Ronald Prasad
Numerade Educator

Problem 7

Early versions of $\mathrm{C}++$ did not support templates, but generic classes could be introduced using parameterized macros. In what respect is the use of templates better than the use of such macros?

Check back soon!
01:12

Problem 8

What is the meaning of private, protected, and public parts of classes?

Jennifer Stoner
Jennifer Stoner
Numerade Educator

Problem 9

What should be the type of constructors and destructors defined in classes?

Check back soon!

Problem 10

Assume the following class declaration:
template<class genType>
class genclass {
. . .
char afunction $(\ldots)$;
$\ldots$; ;
What is wrong with this function definition?
char genclass: : afunction(..) $\{\ldots\}$;

Check back soon!

Problem 11

Overloading is a powerful tool in $\mathrm{C}_{+}+$, but there are some exceptions. What operators must not be overloaded?

Check back soon!
01:18

Problem 12

If classA includes a private variable $n$, a protected variable $m$, and a public variable $k$, and classB is derived from classA, which of these variables can be used in classB? Can $n$ become in class B private? protected? public? How about variables $m$ and $k$ ? Does it make a difference whether the derivation of classB was private, protected, or public?

Jennifer Stoner
Jennifer Stoner
Numerade Educator

Problem 13

Transform the declaration
template<class genType, int size $=50>$
class genclass
gentype storage $[$ size $]$;
...........
void memberFun( ) {
if (someVar $<$ size) $\{\ldots \cdots\}$
$\ldots \ldots \ldots \ldots$
}
} ;
which uses an integer variable size as a parameter to template to a declaration of genclass, which does not include size as a parameter to template and yet allows for flexibility of the value of size. Consider a declaration of genclass's constructor. Is there any advantage of one version over another?

Check back soon!
00:28

Problem 14

What is the difference between function members which are virtual and those which are not?

Zachary Warner
Zachary Warner
Numerade Educator

Problem 15

What happens if the declaration of genclass:
class genclass
$\quad$...............
virtual void process 1 (char);
virtual void process 2 (char);
};
class genclass
..............
virtual void processl(char);
virtual void process 2 (char);
} ;
is followed by this declaration of derivedclass?
class derivedclass : public genclass {
$\ldots \ldots \ldots \ldots \ldots \ldots \ldots$
void processl(int);
int process 2 (char);
};
class derivedclass : public genclass {
..............
void processl(int);
int process 2 (char);
?;
Which member functions are invoked if the declaration of two pointers
$$
\begin{aligned}
& \text { genclass *objectPtr1 = \&derivedclass, } \\
& \text { *objectPtr2 = \&derivedClass; } \\
&
\end{aligned}
$$
is followed by these statements?
objectPtr1->process $1(1000)$;
objectPtr2->process $2($ 'A');

Check back soon!