1、成员函数与对象定义
class Date
{
private:int year;
int month;
int day;
public:void show();
void setdate(int, int, int); //声明函数原型
}date1,date2; //定义类对象
void Date::show() //定义成员函数(注意类外定义成员函数一定
{ //要加“类名::”)
cout << year << month << day << endl;
}
void Date::setdate(int i, int j, int k)
{
year = i; month = j; day = k;
}
或者
class Date
{
private:int year;
int month;
int day;
public:
void show() //定义成员函数
{
cout << year << month << day << endl;
}
void setdate(int i, int j, int k)
{
year = i; month = j; day = k;
}
}
Date date1,date2; //定义类对象
2、构造函数(特殊成员函数)
不用构造函数就只能调用类方法来对对象中成员变量进行初始化,eg.
在class Date
{
private:int year;
int month;
int day;
public:
void show() //定义成员函数
{
cout << year << month << day << endl;
}
void setdate(int i, int j, int k)
{
year = i; month = j; day = k;
}
}
Date date1,date2; //定义类对象
data2.setdata(2000,1,1);
使用构造函数方法:
class Cars
{
private:int wheels;
int doors;
public:
void show() //定义成员函数
{
cout << wheels << doors << endl;
};
Cars(); //声明构造函数
};
Cars::Cars() //定义类外进行构造函数定义
{
wheels = 4; doors = 4;
}
注意:构造函数名称必须与类名相同;
定义时使用“Cars::Cars”;
构造函数被声明为公有函数,在声明类对象的同时被系统自动调用;
构造函数可以有任意类型参数,但不能有返回类型,void 也不行;
带参数的构造函数:
#include<iostream>
using namespace std;
class Cars
{
private:int wheels;
int doors;
public:
void show() //定义成员函数
{
cout << wheels << doors << endl;
};
Cars(int,int); //声明带参数构造函数
};
Cars::Cars(int w,int d) //定义类外进行构造函数定义
{
wheels = w;
doors = d;
}
int main()
{
Cars car(4, 4);
car.show();
system("pause");
return 0;
}
默认参数构造函数(缺省)
(带指针的还不会,可以参考下下面的程序)
#include<iostream>
using namespace std;
class Cars
{
private:int wheels;
int doors;
public:
void show() //定义成员函数
{
cout << wheels << doors << endl;
};
Cars(int w = 4, int d = 4) //定义默认参数构造函数
{
wheels = w; doors = d;
}
};
int main()
{
Cars car1,car2(3),car3(3,3); //分别不传递参数,传递一个参数,两个参数
car1.show();
car2.show();
car3.show();
system("pause");
return 0;
}
拷贝构造函数
#include<iostream>
#include<string>
using namespace std;
class String
{public:
String(const char *s)
{
ptr = new char[strlen(s) + 1];
strcpy(ptr, s);
len = strlen(s);
}
String(const String &S) //拷贝构造函数(指针型)
{
ptr = new char[strlen(S.ptr) + 1];
strcpy(ptr, S.ptr);
len = S.len;
}
~String()
{
delete[]ptr;
}
void show()
{
cout << ptr << endl;
}
private:char *ptr;
int len;
};
int main()
{
String s1("test1");
String s2(s1);
s2.show();
s1.show();
system("pause");
return 0;
}
3、析构函数
作用:在类对象生命周期结束时清理和释放被类对象占用的系统资源
注意:名字:“~构造函数”
析构函数不具有返回类型和参数,不能重载,不能显式调用。
与构造函数不同,一个类只能有一个析构函数。(后面补充)
class Cars
{
private:int wheels;
int doors;
char *carname;
public:
void show() //定义成员函数
{
cout << wheels << doors << endl;
};
Cars(); //声明构造函数
~Cars();
};
Cars::Cars() //定义类外进行构造函数定义
{
wheels = 4; doors = 4;
carname = new char[10]; //分配堆空间
}
Cars::~Cars() //定义析构函数
{
delete[]carname;
cout << "destructed" << endl;
}
3、对象数组
#include<iostream>
using namespace std;
class test
{
private:int x;
public:
void set(int m)
{
x = m;
}
int get()
{
return x;
}
};
int main()
{
test obj[3]; //对象数组定义
int n;
for (n = 0; n < 3; n++)
obj[n].set(n);
for (n = 0; n < 3; n++)
cout << obj[n].get() << endl;
system("pause");
return 0;
}
4、对象指针
引用单个对象
#include<iostream>
using namespace std;
class point
{
private:int x;
int y;
public:
void set(int m,int n)
{
x = m; y = n;
}
void show()
{
cout << x << "," << y << endl;
}
};
int main()
{
point obj, *p;
obj.set(10, 20);
obj.show();
p = &obj;
p->show();
system("pause");
return 0;
}
引用对象数组
#include<iostream>
using namespace std;
class point
{
private:int x;
int y;
public:
void set(int m,int n)
{
x = m; y = n;
}
void show()
{
cout << x << "," << y << endl;
}
};
int main()
{
point obj[2], *p;
obj[0].set(10, 20);
obj[1].set(30, 40);
obj[0].show();
obj[1].show();
p = obj; //引用对象数组时不能用“&“
p->show();
p++; //p++表示指向后一个数组元素
p->show();
system("pause");
return 0;
}
5、this指针
#include<iostream>
using namespace std;
class point
{
private:int x;
int y;
public:
point(int m,int n)
{
x = m; y = n;
}
void show()
{
cout << this->x << "," <<(*this).y << endl;
}
};
int main()
{
point obj(1,2);
point obk(3,4);
obj.show();
obk.show();
system("pause");
return 0;
}