So I am trying to write a class called undergraduate that takes an object of the Phone class as an argument. However, the phone class uses a const data member called msg. This will not compile because of the const data member in the phone class. How do I fix it? Any help will be greatly appreciated!
//Below is my default constructor for the Phone class, it uses const data member msg
Phone::Phone():msg(10){ //intialize const data member
areacode = 999;
exchange = 999;
line = 9999;
}
//here is my constructor that takes objects as arguments
Undergraduate::Undergraduate(Name sname, Date bday, Program sprogram, Phone sphone, char g, string s, int c, double G, int gdyr, string term){
name = sname;
date = bday;
pgm = sprogram;
phone = sphone;
gender = g;
status = s;
credit = c;
gpa = G;
gradyear = gdyr;
gradterm = term;
}
You list the member variables, followed by the value they should be initialized with inside parentheses, separated by comma after the ) and before the {.
This is how it would look like for the Phone constructor.
You only strictly need to do this for the members that you can't assign to later, like Phone::msg and Undergraduate::phone, but it's generally considered the prefered way to initialize member variables so I recommend doing it for all of them.