C++ answers only, C++ CLASSES please dont pick and choose do all 1. What is th d
ID: 3884344 • Letter: C
Question
C++ answers only, C++ CLASSES please dont pick and choose do all
1. What is th diffrnc btwn a class and an instanc of th class?
2. What ar th bnfits of ncapsulation?
3. What is th diffrnc btwn th Prson structur and th Prson class blow?
struct Prson { class Prson{
string nam; string nam;
int ag; int ag;
}; };
4. Considr th following function hadr for a mmbr function.
void Circl::gtRadius()
a.What is th nam of th function?
b. What class dos th function blong to?
5. What is a mutator function?
6. Giv th intrfac dclaration of an accssor function in class Car, calld gtYar, which will rturn th yar th car was mad (an int). Dfin th accssor in a way that nsurs it dos not modify any data mmbrs.
7.Assum th Car class intrfac is stord in fil car.h. Giv th prprocssor dirctivs to nsur that this fil only gts includd onc in a projct.
For qustions 8 – 10 writ a class namd Circl with a singl privat mmbr variabl namd radius (a doubl).
8. Writ an intrfac (or dclaration) for th abov class, with a singl constructor that dfaults th radius to 1. Provid accssors to gt th radius and ara of th circl, and a mutator to st its radius. nsur constnss of any accssors.
9. Writ th class implmntation (or dfinition) that would b stord in fil circl.cpp. Th implmntation should calculat th ara as 3.14159 * radius * radius.
10. Writ a drivr function (namd main.cpp) that uss th abov class. In it
a. Instantiat an objct namd Circ1 that uss th dfault radius.
b. Us cout to print th radius of this circl.
c. Chang th radius of Circ1 to 5.
d. Us cout to print th ara of th modifid circl.
Explanation / Answer
1. A class is a blueprint of the real world entity. It is a user defined data type which contains data and methods to manipulate that data. It does not have any memory allocated. whereas instance is a runtime object of class. When a class is intantiated, an object is created and memory is allocated to one object.
2. Encapsulation is binding the data with the code that manipulates it. It keeps the data and the code safe from external interference. The idea of encapsulation is to keep classes separated and prevent them from having tightly coupled with each other.
3. Members of a Person class are private by default and members of Person struct are public by default.
4. a) Name of function is gtRadius()
b) Name of class is Circl
5. A mutator is a class method used to change data members of the class. eg. setId(int id)
6. int gtYar();
int gtYar() {
return year;
}
7.
For 8,9 and 10 ... below are the codes..
circle.h
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle {
private:
double radius;
public:
Circle() {
radius = 1;
}
double getRadius() const;
void setRadius(double);
double getArea();
};
#endif
circle.cpp
#include "circle.h"
double Circle::getRadius() const{
return radius;
}
void Circle::setRadius(double r) {
radius = r;
}
double Circle::getArea(){
return 3.14159 * radius * radius;
}
main.cpp
#include<iostream>
using namespace std;
#include "circle.cpp"
int main() {
Circle Circ1;
cout<< "radius: "<<Circ1.getRadius()<<endl;
Circ1.setRadius(5);
cout<<"Area of new circle: "<<Circ1.getArea();
}
output
radius: 1
Area of new circle: 78.5397
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.