Lec08 (Topic 4 Define Classes)
Lec08 (Topic 4 Define Classes)
class Student {
public:
Student(); // Default constructor
Student(int id); // Overloaded constructor
Student(string name); // Overloaded constructor
Student(const Student& existingStudent); // Copy constructor
…
};
class Student {
int idNum;
public:
Student() { // default constructor
idNum = 0; // set default value
}
};
int main() {
Student s1; // call default constructor
Student *ps = new Student; // call default constructor
Default Constructors
If you do not provide a default constructor, C++ automatically creates an
empty one (no implementation)
// Your code
class Student { // no default constructor is provided
};
// Compiler code
class Student {
public:
Student() { // compiler's default constructor
// no definition/implementation
}
};
Example: Default Constructor
class Student {
int idNum;
public:
Student() { // default constructor
idNum = 0; // set default value
cout << "A Student object is created!\n";
}
void setIDNum (int id) { Output:
idNum = id;
} A Student object is created!
int getIDNum() { Student id is 0
return idNum; Student id is 123
}
};
int main() {
Student s1; // call default constructor
cout << "Student id = "
<< s1.getIDNum() << endl;
s1.setIDNum (123);
cout << "Student id = "
<< s1.getIDNum() << endl;
return 0;
}
Overloaded Constructors
• Overloaded constructors are constructors that have parameter(s).
• We could provide arguments when creating an instance of a class,
thus invoking an overloaded constructor.
• Overloaded constructors are used to initialize attributes to values
passed as arguments.
class Student {
int idNum;
public:
Student (int id) { // overloaded constructor (1 parameter)
idNum = id; // assign parameter value to attribute
}
};
int main() {
Student s1 (123); // call overloaded constructor
Student *ps = new Student (234); // call overloaded constructor
Overloaded Constructors
• We may provide default arguments for constructor parameters.
• We must follow the same rules as when overloading regular
functions (i.e. parameter lists must differ)
class Student {
int idNum;
public:
Student (int id = 0) { // 2-in-1: alternate default constructor,
// overloaded constructor
idNum = id;
}
};
int main() {
Student s1; // call alternate default constructor
Student *ps = new Student (234); // call overloaded constructor
Constructor Initializer List
• Constructor initializer list allows us to initialize attributes before constructor
body is executed.
• Is placed after the constructor parameter list but before constructor body.
• A single colon (‘:’) is used to separate the parameter list from the initialize list
Parameter is of the
same type as the class
•Destructor has the same name as the class, except that it is preceded by a tilde
‘~’.
class Student {
public:
~Student();
};
Destructor
Destructors are generally used to perform any cleanup necessary prior to
an instance being destroyed. For example deleting dynamically allocated
objects.
// Your code
class Student { // no destructor is provided
};
// Compiler code
class Student {
public:
-Student() { // compiler's default destructor
// no definition/implementation
}
};
Example: Destructor
#include <iostream> int main() {
using namespace std; Student s1(111);
class Student { Student s2(222);
private: return 0;
int idNum; }
public:
Student(int id); // constructor
~Student(); // destructor
}; Output:
Student::Student(int id) { Note: Destroy order reverses
idNum = id; create order
cout << "Object " << idNum
<< " created.\n";
} Object 111 created.
Student::~Student() { Object 222 created.
cout << "Object " << idNum Object 222 destroyed.
<< " destroyed.\n"; Object 111 destroyed.
}
Queries
• Query methods (also called accessors) are methods that are used to
inquire (find out) about the value of an instance’s attributes.
• The key concept with a query method is that it should not change the
value of any of the object’s attributes.
• Types of queries:
• Simple queries (e.g. “What is the value of member x?”)
• Conditional queries (e.g. “Is member x greater than 10?”)
• Derived queries (e.g. “What is the combined value of members x
and y?”)
double Student::getGPA() {
int Test::getXPlusY() {
return gpa;
return (x + y);
}
}
bool Test::isXPositive() {
return (x > 0);
}
Other types of queries should have a descriptive name, and will contain
whatever code is necessary to complete the definition
Updates
• Update methods (also called mutators) contrast with queries. They
change the value of attribute(s) in an object.
• The simplest form of update method will set the value of an attribute to
a specified value, while others will perform much more complex
calculations.
class Student {
const int idNum; // use keyword "const" to declare
const string name; // const attribute
public:
Student (int id, string name) // overloaded constructor
: idNum(id), name(name) // constructor initializer list
{}
};
int main() {
Student s1(123, "Michael");
}
static Attributes
• Static attributes are attributes that are shared across all instances
of a class.
class Student {
static int count; // use keyword "static" to declare
// static attribute
}; // end of class
int Student::count = 0; // initialize count to 0
static Methods
• Static methods operate independently of instances of the class.
To summarize:
• You are able to call only methods that are static or const.
• Compiler will prevent you from changing the value of any non-
static or non-const members (atttibutes and methods).
Example: const Objects
class Student {
public:
Student(int id) { // overloaded constructor
this->id = id;
++count;
}
static int getCount() { // static method
Output:
return count;
} 2
int getId() const { // const method 123
return id;
}
234
private:
static int count; // static attribute
int id; // non-static attribute
};
int Student::count = 0; // initialize static attribute
int main() {
const Student s1(123), s2(234); //2 const instances
cout << Student::getCount() << endl //call static method
<< s1.getId() << endl //call const method
<< s2.getId() << endl; //call const method
return 0;
}