#include <iostream>
using namespace std;
class Clock{
public:
// void setTime(int h){
// hour = h;
// minute = 0;
// }
bool before(const Clock &another) {
// another.hour = 13;
// if (hour < another.hour)
// return true;
// if (hour > another.hour)
// return false;
// if (minute < another.minute)
// return true;
// if (minute >= another.minute)
// return false;
}
void setTime(int h, int m = 0){
setHour(h);
setMinute(m);
// hour = h;
// minute = m;
}
void setHour(int hour){
this->hour = hour;
}
void setMinute(int m){
minute = m;
}
void showTime(){
cout<<hour<<":"<<minute<<endl;
}
private:
int hour, minute;
};
int main()
{
int a = 10;
int c = 20;
const int &b = a;
cout<<a<<" "<<b<<" "<<c<<endl;
a = c;
c = 30;
cout<<a<<" "<<b<<" "<<c<<endl;
// Clock c1;
// c1.setHour(19);
// c1.setMinute(23);
// c1.showTime();
// Clock c2;
// c2.setTime(19,33);
// c2.showTime();
// c1.before(c2);
// c2.showTime();
// cout<<(c1.before(c2)?"true":"false")<<endl;
// Clock c3;
// c3.setTime(20);
// c3.showTime();
// Clock c4;
// c4.setTime();
// c4.showTime();
return 0;
}
#include <iostream>
using namespace std;
class Clock{
public:
// Clock(){hour=minute=0;}
Clock(int h=0, int m=0){
hour = h; minute = m;
cout<<"Clock "<<h<<":"<<m<<" is created."<<endl;
}
~Clock(){//析构函数
cout<<"Clock "<<hour<<":"<<minute<<" is erased."<<endl;
}
void showTime(){
cout<<hour<<":"<<minute<<endl;
}
private:
int hour, minute;
};
int main()
{
// int a = 10;
// Clock c2(20,34),c1;
// Clock *pc = new Clock(20,42);
// c1.showTime();
// pc->showTime();
// delete pc;
Clock clocks[3]={Clock(20,46)};
clocks[2].showTime();
return 0;
}
```cpp
#include <iostream>
using namespace std;//cout是std内定义的一个对象
//定义类关键词:class
class Clock
{
//private://访问控制权限默认private
// int hour,minute;//属性
public://以下是三个类,类名set之后首字母大写,包括数据成员和函数成员(表示行为)
Clock(){hour = minute = 0;}//默认构造函数特点:同名,自动调用,没有返回值
Clock(int h=0, int m=0){
hour = h;
minute = m;
}
bool before(Clock another){//传入another的是数值
if(hour < another.hour)
return true;
if(hour > another.hour)
return false;
if(minute < another.minute)
return true;
if(minute >= another.minute)
return false;
} //布尔类型专门存储真值假值
bool before(Clock &another){
another.hour = 13;
if(hour < another.hour)
return true;
if(hour > another.hour)
return false;
if(minute < another.minute)
return true;
if(minute >= another.minute)
return false;
}
//函数的重载(overload),参数表不一样构成函数重载
void setTime(int h)
{
hour = h;
minute = 0;
}
// void setTime(int h, int m=0)//带默认值的函数,两者等价且不能并存,只传一个参数也能进入。
// {
// hour = h;
// }
void setTime(int h, int m)//同时设置小时分钟
{
setHour(h);
setMinute(m);
//以上等价
// hour = h;
// minute = m;
}
void setHour(int h)//行为:成员函数/方法。
{
this->hour = h;
}
void setMinute(int m)//行为:成员函数/方法。
{
minute = m;
}
void showtime()
{
cout <<hour<<":"<<minute<< endl;
}
private: //访问控制权限默认private
int hour,minute; //属性
};
int main()//main并不关心sethour你要干什么
{
// private 只有私人能用,public公众能用
// cout << "Hello world!" << endl;// << "Hello world!" << 插入到cout,cout输出到屏幕上,endl换行用的。
Clock c1;//定义对象,目前所有成员函数需要通过对象调用,隐含指针this,谁调用谁设this
// c1.setHour(19);
// c1.setMinute(23);
// c1.showtime();
// Clock c2;
// c2.setTime(19,33);
// c2.showtime();
// cout<<(c1.before(c2)?"true":"false")<<endl;
// Clock c3;
// c3.setTime(20);
// c3.showtime();
// Clock c4;
// c4.setTime();
// c4.showtime()
int a = 10;
int &b = a;//&代表引用数据类型,代表b和a是同一个东西,1.引用必须初始化 2.一旦引用完毕不能作为别人的别名,b=c,ab同时改变 3.引用可以作为函数参数
int c = 30;
cout << a<<" "<<b <<" "<<c<< endl;
b = c;
// cout << a<<" "<<b <<" "<<c<< endl;
return 0;
}
在析构函数中
delete name;释放一个
delete[] name;释放多个