Answer to Question #89780 in C++ for matimu

Question #89780
write a program that has a class named "students". the class should have at least two constructors:one being the default constructors and the other one being a copy constructor. and two more functions of your choice.create two objects of type student:one created using the default constructor and the other by the copy constructor.call at least two functions from the class using both object.
1
Expert's answer
2019-05-15T11:56:01-0400
#include <iostream>
using namespace std;


class Students {
  private:
    int size; // number of students


  public:
    Students() {
      this->size = 0;
    }


    Students(const Students &other) {
      this->size = other.getSize();
    }


    void setSize(int size) {
      this->size = size;
    }


    int getSize() const {
      return this->size;
    }
};


int main() {
  // Create object using default constructor
  Students students1;
  students1.setSize(10);
  cout << "Students 1 has size " << students1.getSize() << endl;


  // Create object using copy constructor
  Students students2 = students1;
  students2.setSize(20);
  cout << "Students 2 has size " << students2.getSize() << endl;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS