Question

7. write default and overloaded constructors and destructor for a class Person only the name, age, height weight. one of the constructors name. must initialize 8. What is the purpose of the destructor? Explain with an example. 9. Why do we need Private member Functions
0 0
Add a comment Improve this question Transcribed image text
Answer #1

7.)

#include<iostream>
using namespace std;
class Person {
   public:
       std::string name;
       int age;
       int height;
       int weight;
   public:
       Person(); // defalut constructor
       Person(std::string name_local){ // overloaded constructor which intiates name only.
           name = name_local;
       }
       Person(int age,int height, int weight){ // Overloaded constructor which intiates remaining three.
           age = age;
           height = height;
           weight = weight;
       }
       ~Person(){                       // Destructor for class Person
           cout << "Destructor called";
       }
};
int main(){
}

8.)

"Destructor" of a class in cpp is inverse of constructor functions. They are called when objects are destroyed (deallocated). We declare destructor for a class by preceding the class name with a tilde(~). For Example Person is the class. Then destructor for this class is ~Person().The destructor is commonly used to "clean up" when an object is no longer necessary.

Destructor of a class is used to clear up or to free the memory that is used by that class from intiation of a object until destructor is called. The destructor has a special role in releasing managed and unmanaged resources.

Example :

#include <iostream>

struct Member
{
int index;

Member ( int index ) : index ( index ) {}

~Member()
{
std::cout << "~Member " << index << std::endl;
}
};

int main()
{
Member a1(1);
Member* point;
Member a2(2);
point = new Member(3);
  
delete point; // calls the destructor of a3
}

9.)

The idea of classes is to restrict user access. We don't want the user to access data or functions unnecessarily.

Member data are made private while functions(or methods) are made public. There might be instances where you might want to make certain functions private. You may not want the user to directly access these functions. Private functions can only be called from within public member functions.

Let’s take the example of the class ‘batsman’. After every match the user will enter the batsman’s new score and then he will have to call two functions to update the batsman’s record (i.e. the user has to call update_best ( ) and update_worst ( )). It is unnecessary to bother the user with this kind of a double function call. Why should the user access these two functions directly? Instead of this, we could define another function called update ( ) which will call update_best ( ) and update_worst ( ). In this way the user needs to only call one function after every match.

Add a comment
Know the answer?
Add Answer to:
Water default and overloaded constructor and destructor for a class Person with attributes name, age, height,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Language: C++ Create an abstract base class person. The person class must be an abstract base...

    Language: C++ Create an abstract base class person. The person class must be an abstract base class where the following functions are abstracted (to be implemented in Salesman and Warehouse): • set Position • get Position • get TotalSalary .printDetails The person class shall store the following data as either private or protected (i.e., not public; need to be accessible to the derived classes): . a person's name: std::string . a person's age: int . a person's height: float The...

  • create a java class named Person that has 3 attributes: name, age and weight. include a...

    create a java class named Person that has 3 attributes: name, age and weight. include a default and parametrize constructor. include all setters and getters needed and a method that is used to display the information about the person

  • Using C++ create a class called person. A person can have a name, address, age. For...

    Using C++ create a class called person. A person can have a name, address, age. For this class create a default constructor and parameterized constructor. Show the main as int main() { } ----------------------------------------------------- Show how you will define the class and call it in the main. Only create two instances/objects in main using the two created constructors.

  • python code? 1. Create a class called Person that has 4 attributes, the name, the age,...

    python code? 1. Create a class called Person that has 4 attributes, the name, the age, the weight and the height [5 points] 2. Write 3 methods for this class with the following specifications. a. A constructor for the class which initializes the attributes [2.5 points] b. Displays information about the Person (attributes) [2.5 points] c. Takes a parameter Xage and returns true if the age of the person is older than Xage, false otherwise [5 points] 3. Create another...

  • About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which...

    About Classes and OOP in C++ Part 1 Task 1: Create a class called Person, which has private data members for name, age, gender, and height. You MUST use this pointer when any of the member functions are using the member variables. Implement a constructor that initializes the strings with an empty string, characters with a null character and the numbers with zero. Create getters and setters for each member variable. Ensure that you identify correctly which member functions should...

  • 1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default...

    1. Define a class Rectangle with two attributes: (This is for C++) -length, an integer (default value 1) -width, an integer (default value 1) Provide the following member functions: -default constructor -constructor that takes parameters used to initialize the data -get/set function for each attribute -a function perimeter that returns the perimeter of the rectangle -a function area that returns the area of the rectangle b. Write a program that uses the class Rectangle to create two rectangle objects with...

  • The function should have a class name CPizza that will have three constructors (default, type, and...

    The function should have a class name CPizza that will have three constructors (default, type, and copyl, public functions to use and implement are noted for you in the starter kit, and private member variables (all which are allocated dynamically). The private member variables include the three different sizes of pizza (Large, Medium, and Small), cost, a bool delivery, name, and delivery fee. The prices of the pizzas' are $20. $15, and $10 for large, medium, and small respectively. The...

  • FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width,...

    FOR C++ PLEASE Create a class called "box" that has the following attributes (variables): length, width, height (in inches), weight (in pounds), address (1 line), city, state, zip code. These variables must be private. Create setter and getter functions for each of these variables. Also create two constructors for the box class, one default constructor that takes no parameters and sets everything to default values, and one which takes parameters for all of the above. Create a calcShippingPrice function that...

  • Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and...

    Language: C++ Create a class named 'Salesman' that shall inherit from a class called 'Person' and will add various functionality. We will store the following private variables specific to Warehouse: . a std::vector of float values which indicate the monetary values of each sale made by this salesman . a std::string which stores that salesman's position title . a float which stores their commission percentage, i.e., the percentage of sales they receive as a paycheck . a float which stores...

  • Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed...

    Student class: Instance variables name id Constructors: Default constructor Constructor that has id and name passed to the constructor Methods: Accessors int getID( ) String getName( ) Class Roster: This class will implement the functionality of all roster for school. Instance Variables a final int MAX_NUM representing the maximum number of students allowed on the roster an ArrayList storing students Constructors a default constructor should initialize the list to empty strings a single parameter constructor that takes an ArrayList<Student> Both...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT