首页 / C++ / C++ 类和对象基本认识和创建
C++ 类和对象基本认识和创建
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ 类和对象基本认识和创建,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2696字,纯文字阅读大概需要4分钟。
内容图文
![C++ 类和对象基本认识和创建](/upload/InfoBanner/zyjiaocheng/1323/7424ec4e31524ff6ae8bcc066e587094.jpg)
?
对象赋值语句:
对象1 = 对象2
注意的地方:
对象的类型必须相同。
两个对象之间的赋值,只是数据成员的赋值,而不对成员函数赋值。不同对象的成员函数占有不同的存储空间,而不同对象的成员函数是占用同一个函数代码段,无法对它们赋值。
当类中有 ++
指针 ++
时,需要进行深拷贝。
构造函数:
构造函数是一种特殊的成员函数,它主要用于为对象分配空间,进行初始化。
建立对象的两种形式:
形式1:
类名 对象[(实参表)]
例如:
Date date1(2016, 10, 14);
通过点.符号访问。
形式二:
类名 *指针变量名 = new 类名[实参表];
例如:
Date *pdate = new Date(2016, 10, 14);
这类对象没有名字,成为无名对象,通过指针->访问。
成员初始化列表:
例如:
class Date {
public:
Date(int x, int y, int z);
...
private:
int year;
int month;
int day;
};
Date::Date(int x, int y, int z):year(x), month(y), day(z) {}
带默认参数的构造函数:
例如:
class Coord {
public:
Coord(int a = 0, int b = 0);
...
};
默认参数应该从右边开始向左排。
构造函数是可以重载。
拷贝构造函数:
类名::类名(const 类名 &对象名) {
拷贝构造函数的函数体
}
调用默认构造函数的形式:
带入法:
类名 对象2(对象1);
赋值法:
类名 对象2 = 对象1;
例子:
#include
using namespace std;
class Rectangle {
public:
Rectangle(int len, int wid);
Rectangle(const Rectangle &p);
void showRectangle();
private:
int length;
int width;
};
Rectangle::Rectangle(int len, int wid) : length(len), width(wid) {
cout << "原始构造函数..." << endl;
}
Rectangle::Rectangle(const Rectangle &p) : length(2 * p.length), width(2 * p.width) {
cout << "拷贝构造函数..." << endl;
}
void Rectangle::showRectangle() {
cout << length << " " << width << endl;
}
int main(int argc, const char *argv[]) {
Rectangle test(25, 50);
test.showRectangle();
Rectangle copyTest = test;
copyTest.showRectangle();
cin.get();
return 0;
}
运行:
原始构造函数...
25 50
拷贝构造函数...
50 100
调用拷贝函数的情况:
用类的对象去初始化另一个对象。
Rectangle p2 = p1; // 带入法
Rectangle p2(p1); // 赋值法
当函数的形参是类的对象。
fun1 (Rectangle p) { // 形参是对象 p
p.disp();
}
int main() {
Rectangle p1(10, 20);
fun1(p1); // 调用对象,初始化形参对象p
return 0;
}
当函数返回值是对象,函数执行完后返回调用者时。
Rectangle fun2() {
Rectangle p1(10, 30);
return p1;
}
int main() {
Rectangle p2;
p2 = fun2();
return 0;
}
深拷贝浅拷贝:
深拷贝,另外得分配空间。
例子:
#include
#include
#include
using namespace std;
class StringAB {
public:
StringAB(char *s) {
ptr = new char[strlen(s) + 1];
strcpy(ptr, s);
cout << "构造函数 --- " << ptr << endl;
}
~StringAB() {
cout << "析构函数 --- " << ptr << endl;
delete[]ptr;
}
void show() {
cout << ptr << endl;
}
StringAB &operator = (const StringAB &);
private:
char *ptr;
};
StringAB &StringAB::operator = (const StringAB &pp) {
if (this == &pp) {
return *this;
}
delete[]ptr;
ptr = new char[strlen(pp.ptr) + 1];
strcpy(ptr, pp.ptr);
return *this;
}
int main(int argc, const char *argv[]) {
StringAB book("book"), jeep("jeep");
book.show();
jeep.show();
book = jeep;
book.show();
return 0;
}
运行:
构造函数 --- book
构造函数 --- jeep
book
jeep
jeep
析构函数 --- jeep
析构函数 --- jeep
请按任意键继续. . .
?
?
原文:https://www.cnblogs.com/huvjie/p/14930564.html
内容总结
以上是互联网集市为您收集整理的C++ 类和对象基本认识和创建全部内容,希望文章能够帮你解决C++ 类和对象基本认识和创建所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。