1) Explain the problem with the following code segment and suggest a way to fix it.
int i = 42;
int *p1, *p2;
p1 = &i;
*p2 = *p1;
2) What are the Invariants for the bag class that uses a static array? (Hints: explain the functionality of the class’s private member variables – used and data).
//Q1
int i=42;
int *p1,*p2;
p1=&i;
*p2=*p1;
in this segment of code " *p2=*p1" is wrong.since both p1 and p2 are integer pointer.
we can not assign directly integer value to any pointer without assigning any address.
So we assign addres of any integer value to pointer.we can't assign integer value to it directly.
so last ine i wrong in this code segment.
//Q2
Protected class members and functions can be used inside its class. Protected members and functions cannot be accessed from other classes directly. Additionally protected access specifier allows friend functions and classes to access these data members and functions
Get Answers For Free
Most questions answered within 1 hours.