版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_42528266/article/details/102756279
#include <iostream>
using namespace std;
class Car {
int m_price;
int m_length;
public:
Car(int price = 0, int length = 0) :m_price(price), m_length(length) {
cout << "Car(int price = 0, int length = 0)" << endl;
}
// 拷贝构造函数(格式是固定的)
Car(const Car &car) :m_price(car.m_price), m_length(car.m_length) {
// this->m_price = car.m_price;
// this->m_length = car.m_length;
cout << "Car(const Car &car)" << endl;
}
void display() {
cout << "price=" << this->m_price << ", length=" << this->m_length << endl;
}
};
class Person {
int m_age;
public:
Person(int age = 0) :m_age(age) { }
Person(const Person &person) :m_age(person.m_age) { }
};
class Student : public Person {
int m_score;
public:
Student(int age = 0, int score = 0) :Person(age), m_score(score) { }
Student(const Student &student) :Person(student), m_score(student.m_score) { }
};
int main() {
//Car car1;
//Car car2(100, 5);
//// 利用car2对象创建了car3对象,会调用car3对象的拷贝构造函数进行初始化
//Car car3(car2);
//car3.display();
//cout << "&car2 = " << &car2 << endl;
//cout << "&car3 = " << &car3 << endl;
// Car car2(100, 5);
// Car car3 = car2; // 等价于Car car3(car2)
//Car car2(100, 5);
//Car car3;
// 这里是赋值操作,直接将car2的8个字节数据拷贝给car3的8个字节
// 但这并不是创建新对象,所以不会调用拷贝构造函数
//car3 = car2;
// car3.m_price = car2.m_price;
// car3.m_length = car2.m_length;
getchar();
return 0;
}
#include <iostream>
using namespace std;
// 浅拷贝(shallow copy):指针类型仅仅是拷贝地址值
// 深拷贝(deep copy):拷贝内容到新申请的内存空间
class Car {
int m_price;
char *m_name;
public:
Car(int price = 0, const char *name = NULL) :m_price(price) {
if (name == NULL) return;
// 申请堆空间存储字符串内容
this->m_name = new char[strlen(name) + 1]{};
// 拷贝字符串内容到堆空间(string copy)
strcpy(this->m_name, name);
cout << "Car(int, const char *)" << endl;
}
Car(const Car &car) :m_price(car.m_price) {
if (car.m_name == NULL) return;
// 申请堆空间存储字符串内容
this->m_name = new char[strlen(car.m_name) + 1]{};
// 拷贝字符串内容到堆空间(string copy)
strcpy(this->m_name, car.m_name);
cout << "Car(const Car &car)" << endl;
}
~Car() {
if (this->m_name == NULL) return;
delete[] this->m_name;
this->m_name = NULL;
cout << "~Car()" << endl;
}
void display() {
cout << "price is " << this->m_price << ", name is " << this->m_name << endl;
}
};
int main() {
/*char name[] = { 'b', 'm', 'w', '\0' };
Car *car = new Car(100, name);
car->display();
delete car;*/
Car car1(100, "bmw");
// 将car1的内存空间(8个字节)覆盖car2的内存空间(8个字节)
Car car2 = car1;
cout << endl;
// Car car(100, "bmw");
//char name[] = { 'b', 'm', 'w', '\0' };
//const char *name2 = "bmw";
//cout << name2 << endl;
//// string length
//cout << strlen(name) << endl;
getchar();
return 0;
}
#include <iostream>
using namespace std;
class Car {
int m_price;
public:
Car(int price = 0) :m_price(price) {
cout << "Car(int) - " << this << " - " << this->m_price << endl;
}
Car(const Car &car) :m_price(car.m_price) {
cout << "Car(const Car &) - " << this << " - " << this->m_price << endl;
}
};
//void test1(Car &car) {
//
//}
void test1(Car car) {
}
Car test2() {
Car car(10);
return car;
}
int main() {
// Car car1(10);
// test1(car1);
Car car2 = test2();
// Car car3;
// car3 = test2();
getchar();
return 0;
}
#include <iostream>
using namespace std;
class Person {
public:
Person() {
cout << "Person() - " << this << endl;
}
Person(const Person &person) {
cout << "Person(const Person &person) - " << this << endl;
}
~Person() {
cout << "~Person() - " << this << endl;
}
void display() {
cout << "display()" << endl;
}
};
void test1(Person person) {
}
Person test2() {
// Person person;
return Person();
}
int main() {
Person person1;
person1 = test2();
// Person person;
// Person *p = new Person();
// cout << 1 << endl;
// Person().display();
// cout << 2 << endl;
// Person person;
// test1(person);
// test1(Person());
// Person person = Person();
getchar();
return 0;
}
#include <iostream>
using namespace std;
class Person {
int m_age;
public:
Person() {
cout << "Person() - " << this << endl;
}
explicit Person(int age) :m_age(age) {
cout << "Person(int) - " << this << endl;
}
Person(const Person &person) {
cout << "Person(const Person &person) - " << this << endl;
}
~Person() {
cout << "~Person() - " << this << endl;
}
void display() {
cout << "display() - age is " << this->m_age << endl;
}
};
void test1(Person person) {
}
Person test2() {
return 30;
}
int main() {
// test1(40);
// Person person = test2();
// Person person(10);
// person = test2();
Person person = 10;
// Person person(10);
person = 20;
// person.display();
getchar();
return 0;
}
#include <iostream>
using namespace std;
/*
编译器会为每个类生成一个默认的无参的构造函数
*/
class Person {
public:
int m_age = 0;
};
class Student : public Person {
};
int main() {
Student student;
getchar();
return 0;
}