Representation: How to represent the data in an object
Operation: What operations can be applied to object
class
enumeration
class X{
public:
int m; //data member
int mf(int v){ //function member
int old=m;m=v;return old;
}
}; // Do not forget semi colon!
X var;
var.m=7;
int x=var.mf(9);// what is x?
class X{
private: //private members (only for class member)
//functions
// types
public: //public members
//functions
// types
};
A user cannot directly refer to a private number.
class X{
int a;
};
means
class X{
private:
int a;
};
```c++
So
```c++
X x;
x.a=1;
gives an error. How can I access a?
We have to go through a public function to access private members.
class X{
private:
int a;
public:
int set(int b){a=b;}
int get(){return a;}
}
So
X x;
x.set(1);
cout<<x.get(); //print 1
struct X{
int a;
};
means
struct X{
public:
int a;
};
C version struct does not include member functions.
struct Date{
int y;
int m;
int d;
};
Date today;
To setup an object, today,
today.y=2016;
today.m=2;
today.s=11;
Date x;
x.m=13;
x.d=32;
Does it makes sense? How about leap year?
// We only check year and month.
#include <stdexcept>
void init_day(Date & dd, int y,int m,int d){
if( 0< m && m <= 12){
dd.y=y;
dd.m=m;
dd.d=d;
}
else throw invalid_argument("invalid date!");
}
Ok! But we want to put in Date class. (You cannot use it if y,m,d are private.)
class Date{
public:
Date(int y, int m, int d);//Constructor!
private:
int y,m,d;
};
Let us define a Date member function. Actually, it is called a constructor!
Date::Date(int yy,int mm, int dd) //class::member-function
:y{yy}, m{mm}, d{dd}{} //c++11: member initializer list
We can also define within class as an inline function.
class Date{
public:
Date(int yy, int mm, int dd):y{yy}, m{mm}, d{dd}{}
private:
int y,m,d;
};
The compiler will try to generate code for the function at each point of call.
class Date{
public:
Date(int yy, int mm, int dd){y=yy,m=mm,d=dd;}
private:
int y,m,d;
};
Declare and assign later.
struct X{
const int x;
X(int xx){x=xx;}// Error
}
int main(){
X q{1};//Error
};
We assgined xx later.
struct X{
const int x;
X(int xx):x{xx}{}// OK
};
Date today {2016,2,11};// OK
Date tomorrow; //Error
Why? (No more default Constructor!)
Date::Date(): Date(0,1,1){}//C++11
Of course, we need to declare Date() first.
class Date{
public:
Date(int y,int m,int d);
Date();// Declare!
private:
int y,m,d;
};
class Date{
public:
Date(int yy,int mm,int dd):y{yy},m{mm},d{dd}{}
Date(){}
private:
int y=0,m=1,d=1;
};
class Date{
public:
Date(int yy,int mm,int dd):y{yy},m{mm},d{dd}{}
Date():Date(0,1,1){}
int month(){return m;}
private:
int y,m,d;
};
void f(Date d1, Date d2){
cout<<d1.month()<<" "<<d2.month()<<endl;
}
Make a function print which prints Date.
void print(Date d1);
and
Date d1 {2016,2,11};
print(d1);
// Feb-11, 2016
You need to make extra member functions
int day();
int year();
class Date{
public:
class Invalid{};// A class as exception
Date(int y, int m,int d);
Date():Date(0,1,1){}
int year(){return y;}
int month(){return m;}
int day(){return d;}
private:
int y, m, d;
bool is_valid();
};
Date::Date(int yy,int mm,int dd):y{yy},m{mm},d{dd}
{
if(!is_valid()) throw Invalid{};
}
bool Date::is_valid(){
if(m<1||m>12) return false;
// skip other checkings.
else return true;
}
void f(int m){
try{
Date date {2014,m,2};
}
catch(Date::Invalid){
throw invalid_argument("invalid date");
}
}
get_GPA() returns GPA
0 < = GPA < = 4.0 If not, throw an invalid_argument exception
int main(){
try{
Student taylor {"Taylor","Swift",3.3};
cout<<taylor.get_name()<<endl; // Swift, Talyor
cout<<"GPA="<<taylor.get_GPA()<<endl;
Student john {"John","Doe", -2.1};
cout<<john.get_name()<<endl;
cout<<"GPA="<<john.get_GPA()<<endl;
}
catch(exception & e){
cout<<e.what()<<endl;
}
}