Question

C++ This exercise will introduce static member variables and static methods in class to you. Class...

C++

This exercise will introduce static member variables and static methods in class to you.

Class Department contain information about universities departments:

  • name
  • students amount

in addition it also stores information about overall amount of departments at the university:

  • departments amount
class Department {
public:
    Department(string i_name, int i_num_students);
    ~Department();
    int get_students();    
    string get_name();   
    static int get_total();
private:
    string name;
    int num_students;
    static int total_departments;
};

Carefully read and modify the template.

You have to implement private static variable "total departments" which should contain the counter for the total number of initialised departments objects. In addition implement static public method get_total() Keep in mind that any modifications of this variable should be processed directly inside constructor/destructor.

#include
#include
using namespace std;

class Department {
public:
  
// Constructor definition
Department(string i_name, int i_num_students) {
cout << "Constructor called for: " << i_name << endl;
... // Assigne name and number of students
... // Increase every time object is created
}
  
// Destructor definition
~Department() {
cout << "Destructor called for: " << name << endl;
... // Decrease every time object is created
}
  
  
int get_students() {
...
}
  
string get_name() {
...
}
  
// Static function to return departments count
static int get_total() {
...
}
  
  
private:
string name;
int num_students;
  
// Static private variable 'total_departments'
...
};

// Initialize static member of class Department
int Department::total_departments = 0;

int main(void) {
  
// Create two departments
Department dep1("CS", 2000);
Department dep2("BIO", 3000);
  
  
// Call destructor for dep1
dep1.~Department();
  
// Print total number of objects
cout << "Total departments: " << Department::get_total() << endl;
cout << "Here is the end of the main." << endl;
return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

class Department {

public:

// Constructor definition

Department(string i_name, int i_num_students) {

cout << "Constructor called for: " << i_name << endl;

// Assigne name and number of students

name = i_name;

// Increase every time object is created

num_students = i_num_students;

++total_departments;

}

// Destructor definition

~Department() {

cout << "Destructor called for: " << name << endl;

--total_departments;

}

int get_students() {

return num_students;

}

string get_name() {

return name;

}

// Static function to return departments count

static int get_total() {

return total_departments;

}

private:

string name;

int num_students;

// Static private variable 'total_departments'

static int total_departments;

};

// Initialize static member of class Department

int Department::total_departments = 0;

int main(void) {

// Create two departments

Department dep1("CS", 2000);

Department dep2("BIO", 3000);

// Call destructor for dep1

dep1.~Department();

// Print total number of objects

cout << "Total departments: " << Department::get_total() << endl;

cout << "Here is the end of the main." << endl;

return 0;

}

=============================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
C++ This exercise will introduce static member variables and static methods in class to you. Class...
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
  • C++ edit: You start with the code given and then modify it so that it does...

    C++ edit: You start with the code given and then modify it so that it does what the following asks for. Create an EmployeeException class whose constructor receives a String that consists of an employee’s ID and pay rate. Modify the Employee class so that it has two more fields, idNum and hourlyWage. The Employee constructor requires values for both fields. Upon construction, throw an EmployeeException if the hourlyWage is less than $6.00 or over $50.00. Write a program that...

  • C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member fu...

    C++ Programming Step 1. Design a new ADT that implements a class named Date, add 3 private member variables month, day, year along with their appropriate public constructor / getter / setter member functions inside Date.h and Date.cpp. Step 2. Design another ADT that implements a class named Watch, add 3 private member variables hour, minute, second, along with their appropriate public constructor / getter / setter member functions inside Watch.h and Watch.cpp. Step 3. Next, implement the additional SmartWatch...

  • // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello...

    // Inheriting member functions // Please Make all changes as noted below: // 1) Make sayHello virtual function (hint this will be done in Person class // 2) Change main to create two Person pointer variables // 3) Set each pointer to one of the two objects already instantiated below // 4) Use the pointer variable to invoke the two sayHello() methods #include <iostream > #include <string > using namespace std ; class Person { protected : string name; int...

  • TOE B.2 - 30 pts class Student public Student(0 cout <<"Student0, constructor called." << endl; exp...

    TOE B.2 - 30 pts class Student public Student(0 cout <<"Student0, constructor called." << endl; explicit Student string stuName): stuName(stuName) cout << "Student stuName), constructor called." << endl; -Student) cout<-Student0, destructor called." << endl; string getName) return this->stuName void sayHi0 cout<< "Student: Hi!" << endl 4 virtual void sayHello0 cout <<"Student: Hello!" << endl private string stuName "Student" 5 class CSStudent: public Student f public CSStudent0 0 explicit CSStudent string stuName) -CSStudent) Student) 0 6 cout<<"-CSStudent), destructor called."<<endl; void sayHello)...

  • Using C++, Write a class named Employee that has the following member variables: name. A string...

    Using C++, Write a class named Employee that has the following member variables: name. A string that holds the employee's name. idNumber. An int variable that holds the employee's ID number. department. A string that holds the name of the department where the employee works. position. A string that holds the employee's job title. The class should have the following constructors: A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee's name,...

  • Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower

    Create a class called Flower. Add one member variables color. Add only one getter function for the color. The get function returns color. Implement a constructor that expects color value and assigns it to the member variable. Create a subclass of Flower named Rose. The Rose class has one member variable name.  Add a constructor which expects color and  name. Pass color to the base constructor and set name to it's member variable.Write a program that has an array of ...

  • For the LinkedList class, create a getter and setter for the private member 'name', constructing your...

    For the LinkedList class, create a getter and setter for the private member 'name', constructing your definitions based upon the following declarations respectively: std::string get_name() const; and void set_name(std::string); In the Main.cpp file, let's test your getter and setter for the LinkedLIst private member 'name'. In the main function, add the following lines of code: cout << ll.get_name() << endl; ll.make_test_list(); ll.set_name("My List"); cout << ll.get_name() << endl; Output should be: Test List My List Compile and run your code;...

  • A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double...

    A hard c++ problem ◎Write a c++ program”Student.h”include Private data member, string firstName; string lastName; double GPA(=(4.0*NumberOfAs+3.0*NumberOfBs+2.0*NumberOfCs+1.0*NumberOfDs)/( As+Bs+Cs+Ds+Fs)); Public data member, void setFirstName(string name); string getFirstName() const; void printFirstName() const; void getLastName(string name); string getLastName() const; void printLastName() const; void computeGPA(int NumberOfAs,int NumberOfBs,int NumberOfCs,int NumberOfDs,int NumberOfFs); double getGPA() const; double printGPA() const; A destructor and an explicit default constructor initializing GPA=0.0 and checking if GPA>=0.0 by try{}catch{}. Add member function, bool operator<(const Student); The comparison in this function based on...

  • Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest...

    Use main.cpp, Student.h, Student.cpp (written below) and write classes StudentClub and a non-member function find youngest to make main.cpp compile. Put the declaration and definition of find youngest in StudentClub.h and StudentClub.cpp separately. You may not modify provided files and only submit StudentClub.h and StudentClub.cpp. Non-member function find youngest is declared as follows. It returns the names of students who have the youngest age. Note there may exist more than one students who are youngest. std::vector find_youngest(const std::vector member); StudentClub...

  • c++ Part 1 Consider using the following Card class as a base class to implement a...

    c++ Part 1 Consider using the following Card class as a base class to implement a hierarchy of related classes: class Card { public: Card(); Card (string n) ; virtual bool is_expired() const; virtual void print () const; private: string name; Card:: Card() name = ""; Card: :Card (string n) { name = n; Card::is_expired() return false; } Write definitions for each of the following derived classes. Derived Class Data IDcard ID number CallingCard Card number, PIN Driverlicense Expiration date...

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