Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question 1)=State the output of the this code class Rectangle { float width, len

ID: 3688923 • Letter: Q

Question

Question 1)=State the output of the this code

class Rectangle

{ float width, length;

public:

Rectangle() { width = 3; length = 5; } float area() { return width*length; }

};

int main ()

{ Rectangle r;

cout<<"Area is: " <<r.area(); return 0;

}

Question 2)=Re-write the code changing the area function to a friend function

Question 3)=Give a brief explanation of all of the following concepts. Use an example if you wish.

a) instantiatation of an object            b)      constructor and destructor

c)      data encapsulation                    d)      data hiding

e)      inheritance                                f)       code re-usability

Question 4)=Identify and correct the errors in the following class definitions

class Text { char *st;

int n; public:

void Text(int n1, char *t)

{ st = new int[n1]; strcpy(st,t);

n = n1; delete st;

}

void display() { cout << st; }

};

Explanation / Answer

1.

#include<iostream>
#include<string>
class Rectangle
{ float width, length;
public:
Rectangle() { width = 3; length = 5; } float area() { return width*length; }
};
int main ()
{ Rectangle r;
std::cout<< "Area is: " <<
r.area();
return 0;
}
output: Area is: 15

2.

#include<iostream>
#include<string>
class Rectangle
{
float width, length;
public:
Rectangle() { width = 3; length = 5; }

friend float area(Rectangle);

};
float area(Rectangle d)   
{
d.width=5;   
d.length=3;
return d.width * d.length ;
}
int main ()
{ Rectangle r;
std::cout<< "Area is: " <<
area(r);
return 0;
}
output: Area is: 15

3. A)Instantiatation of an object :In object oriented programing language instantiatation of an object is to create an instance of an object .In c++ we have classes and objects concept to implement one class objects into another classes we go for object creation . using that object we are performing the operations.Another words, this usually means allocating memory for the new object and calling the constructor. The memory allocation might instead be thought as part of pushing the stack frame for objects on the stack.
B)Constructor and destructor : Constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void. Constructors can be very useful for setting initial values for certain member variables.
Destructor is a special member function of a class that is executed whenever an object of it's class goes out of scope or whenever the delete expression is applied to a pointer to the object of that class. A destructor will have exact same name as the class prefixed with a tilde (~) and it can neither return a value nor can it take any parameters. Destructor can be very useful for releasing resources before coming out of the program like closing files, releasing memories etc.
C)Data encapsulation is a mechanism of bundling the data, and the functions that use them and data abstraction is a mechanism of exposing only the interfaces and hiding the implementation details from the user.C++ supports the properties of encapsulation and data hiding through the creation of user-defined types, called classes. We already have studied that a class can contain private, protected and public members. By default, all items defined in a class are private.
D)Data hiding is a characteristic of object-oriented programming. Because an object can only be associated with data in predefined classes or templates, the object can only "know" about the data it needs to know about. There is no possibility that someone maintaining the code may inadvertently point to or otherwise access the wrong data unintentionally. Thus, all data not required by an object can be said to be "hidden."
E)Inheritance:it is the most used oops concept,the main purpose of inheritance is reusability of the source code.here we have two type of classes super classs and sub class ,sub class uses the all property of super class.The idea of inheritance implements the is a relationship. For example, mammal IS-A animal, dog IS-A mammal hence dog IS-A animal as well and so on.
F)code re-usability: it is nothing but the using the already existed code in the program. inheritance,interfaces are mainloy used to code reusability purpose

4.class Text { char *st;
int n; public:
void Text(int n1, char *t)
{ st = new int[n1]; strcpy(st,t);
n = n1; delete st;
}
void display() { cout << st; }

};
error1: member 'Text' has the same name as its class void Text(int n1, char *t)
error2: constructor cannot have a return type void Text(int n1, char *t)
error3: use of undeclared identifier 'cout' void display() { cout << st; }

Corrected outpt:

#include<iostream>
#include<string>
class Text {
char *st;
int n;
public:
void Text1(char n1,char *t){
st = new char[n1];
strcpy(st, t);
n = n1;
delete st;
}
void display() {
std::cout << st;
}
};

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote