C++ Classes – Constructor(s) (Pt.1)
C. Papachristos
Robotic Workers Lab
University of Nevada, Reno
CS-202
Course , Projects , Labs:
Your 3nd Project will be announced today Thursday 2/6.
2nd Project Deadline was this Wednesday 2/5.
Course Week
CS-202 C. Papachristos
Monday | Tuesday | Wednesday | Thursday | Friday | | Sunday |
|
|
| Lab (8 Sections) |
| | |
| CLASS | PASS Session | CLASS |
| | |
PASS Session | | Project DEADLINE | NEW Project | PASS Session | | PASS Session |
Today’s Topics
CS-202 C. Papachristos
C++ Classes Cheatsheet
Constructor(s)
Destructor
Classes
CS-202 C. Papachristos
Class Cheatsheet
Declaration:
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Class (Type) Name
Conventions:
Classes
CS-202 C. Papachristos
Class Cheatsheet
Declaration:
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Access Specifiers
Encapsulation - Abstraction:
Classes
CS-202 C. Papachristos
Class Cheatsheet
Declaration:
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Member Variables
inside a single Code Unit.
Encapsulation - Abstraction:
Conventions:
Classes
CS-202 C. Papachristos
Class Cheatsheet
Declaration:
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Member Function / Class Methods
& Operations
inside a single Code Unit.
Encapsulation - Abstraction:
Conventions:
public Class Interface:
private Class Access:
Classes
CS-202 C. Papachristos
Class Cheatsheet
Usual-case Class Interface Design:
class Car {
public:
float addGas(float gallons);
float getMileage();
bool setEngineTiming(double[16]);
private:
char m_licensePlates[9];
float m_gallons;
float m_mileage;
double m_engineTiming[16];
};
Class Interface to Member Data should “go through” Member Functions.
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Implementation:
class Car {
…
bool addGas(float gallons);
float getMileage();
};
float Car::addGas(float gallons){
/* actual code here */
}
float Car::getMileage(){
/* actual code here */
}
Resolution Operator (::)
An Implementation needs to exist for Class Methods
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Instantiation – Default Construction:
<type_name> <variable_name>;
Car myCar;
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Create (Construct) a variable of specific Class type.
Will employ “Default Constructor”
Object
Note: NOT to be confused with Value-Initialization:
Car myCar = Car();
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object Usage:
<variable_name>.<member_name>;
Car myCar;
float mileage = myCar.getMileage(); strcpy(myCar.m_licensePlates,"Gandalf");
Dot Operator – Member-of
(.)
Member Variables & Member Functions
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object Usage:
<variable_name>.<member_name>;
Car myCar;
float mileage = myCar.getMileage(); strcpy(myCar.m_licensePlates,"Gandalf");
Dot Operator – Member-of
(.)
Member Variables & Member Functions
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object Pointers:
<type_name> * <variable_name_Pt>;
Car myCar;
Car * myCar_Pt;
myCar_Pt = &myCar;
(*myCar_Pt).getMileage();
Works the same as any pointer.
Object
Pointer to Object
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object Pointer Usage:
<variable_name_Pt>-><member_name>;
Car myCar;
Car * myCar_Pt = &myCar;
myCar_Pt->getMileage();
strcpy(myCar_Pt->m_licensePlates,"Gandalf");
Arrow Operator – Member-access
(->)
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object Pointer Usage:
<variable_name_Pt>-><member_name>;
Why?
Chaining Operator Precedence ( . , -> )
(*(*(*topClass).subClass).subSubClass).method();
topClass->subClass->subSubClass->method();
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Arrow Operator – Member-access
(->)
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object in Function – By-Value:
Car myCar;
strcpy(myCar.m_licensePlates,"Gandalf");
printCapPlatesMileage(myCar);
cout << myCar.m_licensePlates;
void printCapPlatesMileage(Car car){
char * lP = car.m_licensePlates;
while (*lP = toupper(*lP)){ ++lP; }
cout << car.m_licensePlates << endl;
cout << car.getMileage() << endl;
}
Note:
Will work with Local Object Copy !
Note:
Will modify Object Data !
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object in Function – By-Reference:
Car myCar;
strcpy(myCar.m_licensePlates,"Gandalf");
printModifyCapPlates(myCar);
cout << myCar.m_licensePlates;
void printModifyCapPlates(Car & car){
char * lP = car.m_licensePlates;
while (*lP = toupper(*lP)){ ++lP; }
cout << car.m_licensePlates << endl;
}
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Note:
Not allowed to modify Object Data !
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object in Function – By-const-Reference:
Car myCar;
strcpy(myCar.m_licensePlates,"Gandalf");
printCapPlates(myCar);
cout << myCar.m_licensePlates;
void printCapPlates(const Car & car){
char * lP = (char*)malloc(sizeof(
car.m_licensePlates));
strcpy(lP,car.m_licensePlates);
char * lP_0 = lP;
while (*lP = toupper(*lP)){ ++lP; }
cout << lP_0 << endl;
}
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Note:
Will modify Object Data !
Classes
CS-202 C. Papachristos
Class Cheatsheet
Class Object in Function – By-Address:
Car myCar;
Car * myCar_Pt = &myCar;
strcpy(myCar_Pt->m_licensePlates,"Gandalf");
printModifyCapPlates(myCar_Pt);
cout << myCar.m_licensePlates;
void printModifyCapPlates(Car * car_Pt){
char * lP = car_Pt->m_licensePlates;
while (*lP = toupper(*lP)){ ++lP; }
cout << car_Pt->m_licensePlates
<< endl;
}
class Car {
public:
float addGas(float gallons);
float getMileage();
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Protection Mechanisms – const Method signature:
Car myCar;
cout << myCar.getMileage() << endl;
cout << myCar.addGas(10.0F) << endl;
float Car::getMileage() const {
return m_mileage;
}
float Car::addGas(float gallons) {
if (m_gallons += gallons > MAX_GALLONS)
m_gallons = MAX_GALLONS;
return m_gallons;
}
A “promise” that Method doesn’t modify Object
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Protection Mechanisms – Access Specifiers:
public
Anything that has access to a Car Object (scope-wise) also has access to all public Member Variables and Functions.
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Protection Mechanisms – Access Specifiers:
private
Members (Variables and Functions) that can ONLY be accessed by Member Functions of the Car Class.
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Protection Mechanisms – Access Specifiers:
protected
Members that can be accessed by:
class Hybrid : Car {
…
float gasToElectricRatio();
};
float Hybrid::gasToElectricRatio(){
if (m_gallons < …){ return …; }
}
A Derived Class
Classes
CS-202 C. Papachristos
Class Cheatsheet
Member Functions – Accessors (“Getters”)
Name starts with get, ends with Member name.
Allows retrieval of non-public Data Members.
float Car::getMileage() const {
return m_mileage;
}
Note: Don’t generally take in arguments.
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Member Functions – Mutators (“Setters”)
Name starts with set, ends with Member name.
Controlled changing of non-public Data Members.
bool Car::setEngineTiming(double t_in[16]){
for (int i=0;i<16;++i){
if (t_in[i]<… || t_in[i]>…){ return false; }
}
for (int i=0;i<16;++i){
m_engineTiming[i]=t_in[i];
}
return true;
}
Note: In simple case, don’t return anything (void).
In controlled setting, return success/fail (bool).
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
CS-202 C. Papachristos
Class Cheatsheet
Member Functions – Facilitators (“Helpers”)
Provide support for the Class’s operations.
float Car::addGas(float gallons) {
if (m_gallons += gallons > MAX_GALLONS)
m_gallons = MAX_GALLONS;
return m_gallons;
}
Note:
public if generally called outside Function.
private/protected if only called by Member Functions.
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons;
float m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[16];
};
Classes
Class Cheatsheet
Classes and Code File Structure
#ifndef CAR_H_
#define CAR_H_
#define NUMVALVES 16
class Car {
public:
float addGas(float gallons);
float getMileage() const ;
char m_licensePlates[9];
protected:
float m_gallons, m_mileage;
private:
bool setEngineTiming(double[16]);
double m_engineTiming[NUMVALVES];
};
#endif
#include <iostream>
#include "Car.h"
#define MAX_GALLONS 20.0
float Car::getMileage() const {
return m_mileage;
}
float Car::addGas(float gallons) {
if (m_gallons += gallons > MAX_GALLONS)
m_gallons = MAX_GALLONS;
return m_gallons;
}
bool Car::setEngineTiming(double t_in[16]){
for (int i=0;i<16;++i){
if (t_in[i]<… || t_in[i]>…) return false;
}
for (int i=0;i<16;++i){
m_engineTiming[i]=t_in[i];
}
return true;
}
Class Header File: Car.h
Class Source File: Car.cpp
CS-202 C. Papachristos
Classes
CS-202 C. Papachristos
#include <iostream>
#include <…>
#include "Car.h"
int main(){
Car myCar;
Car * myCar_Pt = &myCar;
strcpy(myCar_Pt->m_licensePlates,"Gandalf");
printCapPlates(myCar_Pt);
cout << myCar.m_licensePlates << endl;
cout << myCar.getMileage() << endl;
cout << myCar.addGas(10.0F) << endl;
return 0;
}
Program File: car_program.cpp
Class Cheatsheet
Classes and Code File Structure
Note: Compile all your source (.cpp) files together with� g++ car_program.cpp Car.cpp
Constructor(s)
CS-202 C. Papachristos
Constructor – Description
Special Class Methods that “build” an Object.
Remember:
Implicit initialization (Default Constructor)
Implicit: Date myDate;
Explicit: Date myDate (1, 1, 1917);
class Date{
public:
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
But How?
Constructor(s)
CS-202 C. Papachristos
Description
Called when a Class is Instantiated.
Default Constructor:
Overloaded Constructors:
Constructor(s)
CS-202 C. Papachristos
Syntax - General
Syntax:
<class_name>(…);
Date(…);
<class_name>::<class_name>(…){
/* class_name constructor code */
}
Date::Date(…){
/* Date constructor code */
}
class Date{
public:
Date();
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Note: A Special Function!
Constructor(s)
CS-202 C. Papachristos
Syntax – The Default ctor
Default (empty) Constructor:
Date();
Date::Date(){
/* desired default constructor code */
}
Note:
class Date{
public:
Date();
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Note: A Special Function!
Constructor(s)
CS-202 C. Papachristos
Syntax – The Default ctor
Default (empty) Constructor:
Date();
Date::Date(){
m_month = 1;
m_day = 1;
m_year = 1917;
printDay();
/* or even no code at all? */
}
class Date{
public:
Date();
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Note: A Special Function!
Constructor(s)
CS-202 C. Papachristos
Caveats – The Default ctor
Class Instantiation – Default Construction:
Date aDate;
Class Instantiation – Value Initialization:
Date anotherDate = Date();
In the most simplistic case, its behavior�depends on the existence (or not) of a:�“User-declared Constructor”
class Date{
public:
Date();
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Constructor(s)
CS-202 C. Papachristos
Note: A Special Function!
class Date{
public:
Date(int month,
int day, int year);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Syntax – The Parametrized ctor
Overloaded (parametrized) Constructor:
Date(int month, int day, int year);
Date::Date(int month, int day, int year){
m_month = month;
m_day = day;
m_year = year;
printDay();
}
Constructor(s)
CS-202 C. Papachristos
class Date{
public:
Date(int month,
int day, int year);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Implementations
Overloaded (parametrized) Constructor Definition:
Date::Date(int month, int day, int year){
m_month = month;
m_day = day;
m_year = year;
}
Missing: Technically, nothing, but...
Constructor(s)
CS-202 C. Papachristos
class Date{
public:
Date(int month,
int day, int year);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Implementations
Overloaded (parametrized) Constructor Definition:
Date::Date(int month, int day, int year){
if (month>0 && month<=12) {� m_month = month; }
else { m_month = 1; }
if (day>0 && day<=31) {
m_day = day; }
else { m_day = 1; }
if (year>=1917 && year<=2017) {
m_year = year; }
else { m_year = 1; }
}
Constructor(s)
CS-202 C. Papachristos
class Date{
public:
Date(int month,
int day, int year);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Implementations
Overloaded (parametrized) Constructor Definition:
Date::Date(int month, int day, int year){
if (month>0 && month<=MAX_MONTH) {� m_month = month; }
else { m_month = 1; }
if (day>0 && day<=MAX_DAY) {
m_day = day; }
else { m_day = 1; }
if (year>=MIN_YEAR && year<=MAX_YEAR) {
m_year = year; }
else { m_year = 1; }
}
Constructor(s)
class Date{
public:
Date(int month,
int day, int year);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Implementations
Overloaded (parametrized) Constructor Definition
(Controlled /w even better coding):
Date::Date(int month, int day, int year){
setMonth(month);
setDay(day);
setYear(year);
}
Why?
CS-202 C. Papachristos
Constructor(s)
Overloading
Can have multiple versions of the Constructor:
Different constructors for when:
Note:
Date myDate;
CS-202 C. Papachristos
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day, int year);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Constructor(s)
Overloading
Can have multiple versions of the Constructor:
Different constructors for when:
Note:
CS-202 C. Papachristos
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day, int year);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Constructor(s)
Overloading
Can have multiple versions of the Constructor:
Constructor invoked with full range of arguments:
Date::Date(int month, int day, int year){
setMonth(month);
setDay(day);
setYear(year);
}
CS-202 C. Papachristos
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day, int year);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Constructor(s)
Overloading
Can have multiple versions of the Constructor:
Constructor invoked with no arguments:
Date::Date(){
setMonth(DEFAULT_MONTH);
setDay(DEFAULT_DAY);
setYear(DEFAULT_YEAR);
}
CS-202 C. Papachristos
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day, int year);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Constructor(s)
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day, int year);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Overloading
Can have multiple versions of the Constructor:
Constructor invoked with some arguments:
Date::Date(int month, int year){
setMonth(month);
setDay(DEFAULT_DAY);
setYear(year);
}
CS-202 C. Papachristos
Constructor(s)
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Overloading Caveats
Consider 2 Overloaded Constructor versions:
Date::Date(int month, int year){
setMonth(month);
setDay(DEFAULT_DAY);
setYear(year);
}
Date::Date(int month, int day){
setMonth(month);
setDay(day);
setYear(DEFAULT_YEAR);
}
CS-202 C. Papachristos
Constructor(s)
class Date{
public:
Date();
Date(int month,
int year);
Date(int month,
int day);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Overloading Caveats
Consider 2 Overloaded Constructor versions:
What the Compiler “sees”:
Date::Date(int month, int year){ /* … */ }
Date(int , int);
Date::Date(int month, int day){ /* … */ }
Date(int , int);
CS-202 C. Papachristos
Function Prototype Signature
Function Prototype Signature
Constructor(s)
CS-202 C. Papachristos
Default Parameters
Not really meaningful to have numerous Constructors just to set default Member values.
Function Prototype Syntax:
Date(int month, int day=DFLT_D,
int year=DFLT_Y, bool printFlg=false);
class Date{
public:
Date();
Date(int month,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Note:
Constructor(s)
CS-202 C. Papachristos
Default Parameters
Not really meaningful to have numerous Constructors just to set default Member values.
Function Prototype Syntax (NO!) :
Date(int month=DFLT_M, int day=DFLT_D,
int year=DFLT_Y, bool printFlg=false);
Date();
class Date{
public:
Date();
Date(int month=DFLT_M,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Attention: Same Prototype Signature !
Date d;
error: call of overloaded ‘Date()’� is ambiguous
Constructor(s)
CS-202 C. Papachristos
Default Parameters
Function Implementation Syntax:
Date::Date(int month, int day, int year,
bool printFlg){
setMonth(month);
setDay(day);
setYear(year);
if (printFlg){ printDay(); }
}
class Date{
public:
Date();
Date(int month,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Note:
Constructor(s)
CS-202 C. Papachristos
Default Parameters
Call with Default Parameters:
Date defaultCtorDate;
Date myBDayPrinted(5, 15, 1985, true);
Date myBDay(5, 15, 1985);
Date halloween(10, 31);
Date july(4);
// defaultDate: 4196816/0/4196304
// myBDayPrinted: 5/15/1985
// myBDay: 5/15/1985
// halloween: 10/31/1917
// july: 4/1/1917
class Date{
public:
Date();
Date(int month,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
Output: 5,15,1985
Default Constructor call
(no parameter list)
Constructor(s)
CS-202 C. Papachristos
Default Parameters
Call with Default Parameters – Caveats:
Date(int month, int day=DFLT_D,
int year=DFLT_Y, bool printFlg=false);
No skipping Parameters! Can only do:
Date myFullDatePrinted(2, 9, 2017, true);
Date myFullDate(2, 9, 2017);
Date myMonthDayOnly(2, 9);
Date myMonthOnly(2);
class Date{
public:
Date();
Date(int month,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month;
int m_day;
int m_year;
};
or
or
or
Constructor(s)
CS-202 C. Papachristos
class Date{
public:
Date();
Date(int month,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
Date(const Date & other);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month, m_day,
m_year;
};
Syntax – The Copy ctor
Copy (class-object) Constructor:
Date(const Date & );
Date::Date(const Date & other){
/* Date date object-const-ref copy code */
}
Note:
Constructor(s)
CS-202 C. Papachristos
Implementations
Copy (class-object) Constructor Definition:
Date::Date(const Date & other){
m_month = other.m_month;
m_day = other.m_day;
m_year = other.m_year;
}
Same Class:
class Date{
public:
Date();
Date(int month,
int day=DFLT_D,
int year=DFLT_Y,
bool printFlg=false);
Date(const Date & other);
void setMonth(int m);
void setDay(int d);
void setYear(int y);
void printDay() const;
void shiftNextDay();
private:
int m_month, m_day,
m_year;
};
Destructor(s)
CS-202 C. Papachristos
Destructor – Description
Called when a Class goes out-of-scope or is freed from the heap (by delete).
Destructor
Destructor will automatically (without writing any code in it) call Destructor of any contained Data Member Objects.
– More about that in Lectures later on … –
Classes
CS-202 C. Papachristos
Designing a Class
Ask yourself:
Beginner’s rules of thumb:
Time for Questions !
CS-202
CS-202 C. Papachristos