this is the c++ Course Write the definition of a class that has the following pr
ID: 665079 • Letter: T
Question
this is the c++ Course
Write the definition of a class that has the following properties a. The name of the class is secretType. b. The class secretType has four member variables: name of type string, age and weight of type int, and height of type double The class secretType has the following member functions. (Make each accessor function constant.) print outputs the data stored in the member variables with the appropriate titles setName-function to set the name setAge function to set the age setWeight-function to set the weight setHeight-function to set the height getNamevalue-returning function to return the name getAge-value-returning function to return the age getweight-value-returning function to return the weight getHeight-value-returning function to return the height constructor-with default parameters: The default value of name is the empty string " ", and the default values of age, weight, and height are 0 Write the definition of the member functions of the class secretType, as described in Part c. c. d.Explanation / Answer
Have derived two approaches for this program :
1st Method :
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
class secretType
{
public:
// constructor with parameters
secretType secretObject(string = " ", int = 0, int = 0, double = 0);
void print() const; //prints for testing
void setName(string name);
void setAge(int age);
void setWeight(int weight);
void setHeight(double height);
string getName()const;
int getAge()const;
int getWeight()const;
double getHeight()const;
private:
string name;
int age, weight;
double height;
};
secretType yourName;
yourName.setName();
yourName.setAge();
yourName.setWeight();
yourName.setHeight();
yourName.getName();
yourName.print();
system("PAUSE");
return EXIT_SUCCESS;
}
secretType::secretType(string name, int age, int weight, double height)
{
name = " ";
age = 0;
weight = 0;
height = 0;
}
void secretType:: setName()
{
cout << "enter your name: "; cin >> name;
}
void secretType::setAge()
{
cout << "enter your age: "; cin >> age;
}
void secretType:: setWeight()
{
cout << "enter your weight: "; cin >> weight;
}
void secretType::setHeight()
{
cout << "enter your height in decimal format(0ft.0in): "; cin >> height;
}
string secretType:: getName()const
{
return name;
}
int secretType:: getAge() const
{
return age;
}
int secretType:: getWeight() const
{
return weight;
}
double secretType:: getHeight() const
{
return height;
}
void secretType:: print() const
{
cout << "name: " << name << " age: " << age << " weight: " << weight << " height: " << height << endl;
}
Second Approach :
#include <iostream>
using namespace std;
class secretType
{
public:
// constructor with parameters
secretType secretObject(string = " ", int = 0, int = 0, double = 0);
void print() const; //prints for testing
void setName(string name);
void setAge(int age);
void setWeight(int weight);
void setHeight(double height);
string getName()const;
int getAge()const;
int getWeight()const;
double getHeight()const;
private:
string name;
int age, weight;
double height;
};
// class member function definitions
void secretType::print() const
{
cout << "Name: " << name <<endl;
cout << "Age: " << age <<endl;
cout << "Weight: " << weight <<endl;
cout << "Height: " << height <<endl;
}
void secretType::setAge(int a)
{
age = a;
}
void secretType::setName(string n)
{
name = n;
}
void secretType::setWeight(int w )
{
weight = w;
}
void secretType::setHeight(double h)
{
height = h;
}
string secretType::getName()const
{
return name;
}
int secretType::getAge()const
{
return age;
}
int secretType::getWeight()const
{
return weight;
}
double secretType::getHeight()const
{
return height;
}
int main()
{
secretType secretObject; // create secretType class object to test
secretObject.setName("Brent Smith");
secretObject.setAge(36);
secretObject.setWeight(228);
secretObject.setHeight(78);
secretObject.print();
system("PAUSE");
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.