• wonderlic tests
  • EXAM REVIEW
  • NCCCO Examination
  • Summary
  • Class notes
  • QUESTIONS & ANSWERS
  • NCLEX EXAM
  • Exam (elaborations)
  • Study guide
  • Latest nclex materials
  • HESI EXAMS
  • EXAMS AND CERTIFICATIONS
  • HESI ENTRANCE EXAM
  • ATI EXAM
  • NR AND NUR Exams
  • Gizmos
  • PORTAGE LEARNING
  • Ihuman Case Study
  • LETRS
  • NURS EXAM
  • NSG Exam
  • Testbanks
  • Vsim
  • Latest WGU
  • AQA PAPERS AND MARK SCHEME
  • DMV
  • WGU EXAM
  • exam bundles
  • Study Material
  • Study Notes
  • Test Prep

PROGRAMMING USING C - 1. The following assignments are illegal: (b)p ...

Testbanks Dec 30, 2025 ★★★★☆ (4.0/5)
Loading...

Loading document viewer...

Page 0 of 0

Document Text

Chapter 1

OBJECT-ORIENTED

PROGRAMMING USING C++

1. The following assignments are illegal:

(b)p = *&i; // cannot convert int to int* (c)p = &*i; // invalid indirection (i is not a pointer) (e)i = *&p; // cannot convert int* to int (f)i = &*p; // cannot convert int* to int (h)q = *&*p; // cannot convert int to int* (i)q = **&p; // cannot convert int to int*

  • (a) Functionf()returns the reference to a local variable; upon exit, the variable is destroyed, so that
  • the returned reference refers to a non-existing variable.(b) Before copyings2tos1, enough room has to be allocated to accommodate the contents ofs1, as in char *s1 = new char[strlen(s2)+1]; strcpy(s1,s2); (c) When allocating room for the contents ofs2, one more character has to be allocated for the end-of-string character.

3. Assuming that these declarations have been made:

int intArray[] = {1, 2, 3}, *p = intArray; then (a)*p++;returns 1 and then increments pointerp;intArrayremains unchanged.(b)(*p)++;returns 1 and then increments 1 in the rst cell to 2, so thatintArraybecomes .(c) Statements*p++; (*p)++;cause 1 to be returned, pointerpto be incremented so that the second statement returns 2 and then increments it to 3;intArraybecomes .

  • (a)// A function to add all numbers in an integer array;
  • int sumIntegerArray(int *intArray, int size) { int *last = intArray + size, sum; for (sum = 0; intArray < last; intArray++) sum += *intArray; return sum; } 1 Data Structures and Algorithms in C++ 4e Adam Drozdek (Solutions Manual All Chapters, 100% Original Verified, A+ Grade) 1 / 4

(b)// [2 4 5 7 8 9 10] ==> [2 4 8 10 10 10 10] int removeOddNumbers1(int *intArray, int size) { int *localArray = new int[sizeof(int) * size], i, j, last; for (i = j = 0; i < size; i++) if (*(intArray + i) % 2 == 0) // copy even numbers to localArray; *(localArray + j++) = *(intArray + i); for (i = 0; i < j; i++) // copy even numbers from localArray *(intArray + i) = *(localArray + i); // back to intArray and for (last = *(intArray+i-1); i < size; i++) // pad the ending places *(intArray + i) = last; // with the largest even number of intArray; return j; // return number of even numbers without the trailing ones; } In the case of an unordered array, in one pass through the array, cluster all even numbers at the beginning of the array by using two pointers, to scan the array forward and backward. After the forward pointer nds an odd number and the backward pointer locates an even number a swap occurs. The process stop after forward pointer crosses the backward pointer. Then, return an integer indicating how many even numbers there are in the array.

  • (a)int Strlen(char *s) {
  • char *tmp = s; while (*s++ != '\0'); return s - tmp - 1; } (b)int Strcmp(char *s1, char *s2) { for ( ; *s1 != '\0' && *s1 == *s2; s1++, s2++); return *s1 - *s2; } (c)char* Strcat(char *s1, char *s2) { char *s = s1 + strlen(s1); while (*s++ = *s2++); return s1; } (d)char* Strchr(char *s, char ch) { for ( ; *s != ch && *s != '\0'; s++);

return *s == ch ? s : 0;

}

  • In the condition(p == q), values of pointers are compared to check whether they point to the same
  • location, in the condition(*p == *q), contents of locations pointed to by the two pointers are com- pared.

  • All macros are handled by a preprocessor and inserted in programs with no consideration of types and
  • context, unlike templates which are compiled. Also, templates are a natural generalization of classes, which is lost when macros are used.

  • Function and data members declaredprivatein a classCare accessible only to other members of
  • Cand to functions declared byCasfriends. Function and data members declared protectedare accessible to any class derived fromCand tofriendfunctions. Function and data members declared publiccan be accessed by any function.

  • It is illegal to specify a return type for constructors and destructors, even if it is to bevoid.

10. Providing that the following declaration has been made:

template

2 2 / 4

class genClass { ...char aFunction(...);

... };

the denition char genClass::aFunction(...) { ... } is improper, since the class is notgenClassbutgenClass. Also, sinceaFunctionby being a part of a generic class becomes itself generic, it requires a template as well, hence its declaration should be template char genClass::aFunction(...) { ... }

  • The following operators cannot be overloaded: 1. class member,., 2. class member dereference,.*,
  • conditional,?:, scope resolution,::, andsizeof.

  • For any of the three derivation modes, aprivatevariable remainsprivateto the base class and as
  • such invisible in derived classes, hencencannot be used inclassB. Inpublicderivation, eachpublic andprotectedvariable retains its status, e.g.,publicvariable inclassAremainspublicinclassB.Inprotectedderivation, bothpublicandprotectedvariables of base class areprotectedin derived class. Inprivatederivation, bothpublicandprotectedvariables of base class areprivatein derived class. However, the status of some variables can be adjusted to the status this variable has in the base class. For example, inprivatederivation, theprotectedvariablemcan remain such if listed in the protectedsection asclassA::m, and thepublicvariablekcan be redeclared aspublic(but not as protected).

13. A new declaration ofgenClassis:

template class genClass2 { T *storage;

..................

int size; void memberFun() {

............

if (someVar < size) { ...... }

............

}

public:

genClass2(int n = 50) { storage = new T[size = n]; ... } };

and then declarations of objects is:

genClass2 intObject1(); // use the default size; genClass2 intObject2(25); Ifsizeis a parameter totemplate,storageallocation is static, if it is a parameter of a stack constructor, the allocation is dynamic. Therefore, forsomeFun()declared as int someFun(int size, ... ) { genClass ob1; genClass2 ob2(size);

......................

}

3 3 / 4

the rst declaration is impossible, sincegenClassrequiressizeto be a constant, not a variable, which is permissible in the case ofgenClass.

  • Forvirtualfunctions, the system decides at run-time which function should be invoked, whereas
  • in the case of nonvirtualfunction members this decision is made at compile time. The dierence betweenvirtualand nonvirtualmeans the dierence between run time binding and compile time binding.

15. If declarations:

class genClass {

..................

virtual void process1(char); virtual void process2(char); };

...............

class derivedClass : public genClass {

..................

void process1(int); int process2(char); };

...............

genClass *objectPtr1 = &derivedClass, *objectPtr2 = &derivedClass;

are followed by these statements:

objectPtr1->process1(1000); objectPtr2->process2('A'); then sinceprocess1()is declared inderivedClassas a function with an integer parameter, it hides the declaration ofprocess1()ingenClass, hence,genClass::process1()andderivedClass::process1() are two dierent member functions. In this situation, the functiongenClass::process1()is invoked by the statementobjectPtr1->process1(1000);, sinceobjectPtr1is of typegenClass*, number 1000 is converted into a character type, which means that it is truncated. On the other hand, the dec- laration ofprocess2()inderivedClasschanges return type ofvirtualfunctionprocess2()dened ingenClass, which causes a compile time error.

  • / 4

User Reviews

★★★★☆ (4.0/5 based on 1 reviews)
Login to Review
S
Student
May 21, 2025
★★★★☆

I was amazed by the comprehensive coverage in this document. It enhanced my understanding. Truly excellent!

Download Document

Buy This Document

$1.00 One-time purchase
Buy Now
  • Full access to this document
  • Download anytime
  • No expiration

Document Information

Category: Testbanks
Added: Dec 30, 2025
Description:

Chapter OBJECT-ORIENTED PROGRAMMING USING C++ 1. The following assignments are illegal: (b)p = *&i; // cannot convert int to int* (c)p = &*i; // invalid indirection (i is not a pointer) (e)i = *&p;...

Unlock Now
$ 1.00