Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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. c. 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 getName—value-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. d. Write the definition of the member functions of the class secretType, as described in Part c.

Short Answer

Expert verified
Define `secretType` with private variables, a constructor, and member functions including setters, getters, and a print method.

Step by step solution

01

Define the class structure

Start by defining the class `secretType` and including the private member variables. Use the `private` access specifier for data encapsulation. ```cpp class secretType { private: std::string name; int age; int weight; double height; public: // Public member functions will be defined here. }; ```
02

Define the constructor

Create the constructor for the class with default parameters as specified: an empty string for `name`, and zero for `age`, `weight`, and `height`. ```cpp secretType(std::string n = "", int a = 0, int w = 0, double h = 0) : name(n), age(a), weight(w), height(h) {} ```
03

Implement setter functions

Implement the setter member functions to modify the member variables. ```cpp void setName(std::string n) { name = n; } void setAge(int a) { age = a; } void setWeight(int w) { weight = w; } void setHeight(double h) { height = h; } ```
04

Implement getter functions

Create constant member functions to return the values of the member variables. ```cpp std::string getName() const { return name; } int getAge() const { return age; } int getWeight() const { return weight; } double getHeight() const { return height; } ```
05

Implement the print function

Define the `print` function to display the values of member variables with appropriate titles. ```cpp void print() const { std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Weight: " << weight << std::endl; std::cout << "Height: " << height << std::endl; } ```
06

Finalize the class definition

Combine all the components into the final class definition for `secretType`. ```cpp class secretType { private: std::string name; int age; int weight; double height; public: secretType(std::string n = "", int a = 0, int w = 0, double h = 0) : name(n), age(a), weight(w), height(h) {} void setName(std::string n) { name = n; } void setAge(int a) { age = a; } void setWeight(int w) { weight = w; } void setHeight(double h) { height = h; } std::string getName() const { return name; } int getAge() const { return age; } int getWeight() const { return weight; } double getHeight() const { return height; } void print() const { std::cout << "Name: " << name << std::endl; std::cout << "Age: " << age << std::endl; std::cout << "Weight: " << weight << std::endl; std::cout << "Height: " << height << std::endl; } }; ```

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

Key Concepts

These are the key concepts you need to understand to accurately answer the question.

Member Functions
In C++ programming, a class is a blueprint for creating objects. It includes both data and functions that act on the data. These functions are known as member functions. They provide the means to perform operations on the data and modify the object's state.
A great example of member functions can be seen in the `secretType` class, which includes setter functions like `setName`, `setAge`, `setWeight`, and `setHeight`. These functions allow you to change the values of the member variables. Additionally, there are getter functions such as `getName`, `getAge`, `getWeight`, and `getHeight`. These provide a way to access values without modifying them.
Member functions are usually declared in the `public` section of a class. This makes them accessible from outside the class, allowing interaction with the object's data while maintaining the data's integrity through controlled access.
Data Encapsulation
Encapsulation is one of the fundamental principles of object-oriented programming. It involves bundling the data (variables) and methods (functions) into a single unit, or class, and restricting outside access to some of the object's components. This is crucial for maintaining the integrity of the data and preventing unauthorized interference.
In the `secretType` class, encapsulation is achieved by marking member variables like `name`, `age`, `weight`, and `height` as `private`. By doing so, direct access to these variables from outside the class is not allowed. Only the class's member functions can modify or retrieve these values.
This concept helps in protecting the internal state of an object. Changes can only be made through specific functions (like the setter functions), ensuring that the data remains valid and consistently managed.
Constructor with Default Parameters
A constructor is a special type of member function that is automatically called when an object is created. It typically initializes the object's member variables and prepares the new object for use. In C++, constructors can have default parameters.
The `secretType` class features a constructor with default parameters such as an empty string for `name`, and zero for `age`, `weight`, and `height`. This allows for creating objects with default settings if specific values are not provided at the time of creation. For example:
  • `secretType person;` creates a `secretType` object with default values.
  • `secretType person("John", 30, 70, 175.5);` creates a `secretType` object with specified values.
Default parameters can be very helpful in simplifying object instantiation. They provide flexibility, making it easy to create objects with necessary initial values without overloading constructors.
Object-Oriented Programming
Object-oriented programming (OOP) is a paradigm centered around objects rather than actions and data rather than logic. C++ is one of many languages that adopt OOP principles. It utilizes classes and objects to allow developers to model real-world entities within software.
The class `secretType` is a clear demonstration of OOP. The class acts as a blueprint for creating instances known as objects. Each object of `secretType` can have its own values for `name`, `age`, `weight`, and `height`, providing an organized way to manage and utilize data.
OOP promotes code reusability through inheritance, allows for data protection with encapsulation, and supports an easy-to-understand structured design. Its principles contrast with procedural programming, focusing on splitting operations into procedures rather than objects. This paradigm shift dramatically improves software modularity, maintenance, and scalability.

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

What is a constructor? Why would you include a constructor in a class?

Find the syntax errors in the following class definition: class secret //Line 1 { //Line 2 public: //Line 3 bool compare(); //Line 4 void print() const; //Line 5 secret(int = 0, int = 0) const; //Line 6 private: //Line 7 string str; //Line 8 int one; //Line 9 int two; //Line 10 }; //Line 11

What is the main difference between a struct and a class?

Mark the following statements as true or false. a. The member variables of a class must be of the same type. b. The member functions of a class must be public. c. A class can have more than one constructor. d. \(A\) class can have more than one destructor. e. Both constructors and destructors can have parameters.

Consider the following statements: class temporary { public: void set(string, double, double); void print(); double manipulate(); void get(string&, double&, double&); void setDescription(string); void setFirst(double); void setSecond(double); string getDescription() const; double getFirst()const; double getSecond()const; temporary(string = "", double = 0.0, double = 0.0); private: string description; double first; double second; }; a. How many members does class temporary have? b. How many private members does class temporary have? c. How many constructors does class temporary have? Can this constructor be used to initialize an object without specifying any parameters? If yes, then illustrate with an example; otherwise, explain why it cannot be used to initialize an object witout specifying any parameters.

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free