C++基礎一
阿新 • • 發佈:2018-11-16
main.cpp
#include <Teacher.h>
#include <iostream>
using namespace std;
void fun() {
MyTeacher teacher;
teacher.setAge(18);
teacher.setName("小明");
cout << "teacher name : " << teacher.getName() << endl;
cout << "teacher age : " << teacher.getAge() << endl;
}
//值傳遞
void setFunc(MyTeacher teacher) {
teacher.setAge(16);
teacher.setName("jhone");
cout << "teacher name : " << teacher.getName() << endl;
cout << "teacher age : " << teacher.getAge() << endl;
}
//引用傳遞
void setFuncY(MyTeacher &teacher) {
teacher.setAge(16);
teacher.setName("jhone");
cout << "teacher name : " << teacher.getName() << endl;
cout << "teacher age : " << teacher.getAge() << endl;
}
void main() {
//fun();
//MyTeacher teacher;
//teacher.setAge(20);
//teacher.setName("小明");
MyTeacher teacher = MyTeacher("xiaohong", 25);//沒new關鍵字的是物件
MyTeacher *teacher1 = new MyTeacher("xiaohong", 25);//有new關鍵字是指標 不會走虛構函式 需要自己釋放
delete teacher1;//釋放物件 才會走虛構函式
teacher1 = nullptr;
cout << "teacher name : " << teacher.getName() << endl;
cout << "teacher age : " << teacher.getAge() << endl;
setFuncY(teacher);
cout << "teacher name : " << teacher.getName() << endl;
cout << "teacher age : " << teacher.getAge() << endl;
system("pause");
}
Teacher.h
Teacher.cpp#pragma once class MyTeacher { public: MyTeacher();//建構函式 (malloc) ~MyTeacher();//虛構函式 釋放在建構函式裡面動態申請的記憶體 (free) void setAge(int age); int getAge(); void setName(char *name); char* getName(); private: int age; char *name; };
#include <Teacher.h>
#include<iostream>
using namespace std;
//建立名稱空間
namespace NSP_A {
struct MyStudent
{
int age;
};
}
MyTeacher::MyTeacher()
{
std::cout << "MyTeacher 建構函式 " << this << std::endl;
}
MyTeacher::~MyTeacher()
{
//使用了std的名稱空間 就可以不要前面的std限定符
cout << "MyTeacher 虛構函式 " << this << std::endl;
}
void MyTeacher::setAge(int age) {
this->age = age;
//名稱空間的使用
NSP_A::MyStudent t;
t.age = 18;
using NSP_A::MyStudent;
MyStudent t2;
t2.age = 16;
}
int MyTeacher::getAge() {
return this->age;
}
void MyTeacher::setName(char *name) {
this->name = name;
}
char* MyTeacher::getName(){
return this->name;
}
淺拷貝和深拷貝
MyStudent.h
class MyStudent
{
public:
MyStudent(int age,char*name,char *teacherName);
~MyStudent();
//重寫預設的拷貝建構函式
MyStudent(const MyStudent &student);
public:
int age;
char *name;
char *teacherName;
private:
};
MyStudent.cpp
#include <MyStudent.h>
#include <iostream>
using namespace std;
MyStudent::MyStudent(int age, char*name, char *teacherName):age(age),name(name),teacherName(teacherName)
{
cout << "無參建構函式" << this << endl;
}
MyStudent::~MyStudent()
{
cout << "虛構函式" << this << endl;
}
//預設拷貝建構函式 淺拷貝
MyStudent::MyStudent(const MyStudent &student) {
cout << "拷貝建構函式" << this << endl;
this->age = student.age;
this->name = student.name;
this->teacherName = student.teacherName;
}
main.cpp
void copyTest() {
MyStudent student = MyStudent(21, "jake", "jhone");
MyStudent stu2 = student;//也會呼叫拷貝建構函式
}9:58 2017/10/23
void main() {
copyTest();
system("pause");
}
注意:淺拷貝可能引出的問題 我在建構函式申請一塊記憶體, 虛構函式去釋放這塊記憶體 當我退出時釋放記憶體 由於拷貝的物件是執行的拷貝建構函式 這裡沒有申請記憶體 用的是同一塊記憶體 這塊記憶體釋放後 執行拷貝物件虛構函式時 會丟擲異常 是由野指標造成的
問題產生
#include <MyStudent.h>
#include <iostream>
using namespace std;
MyStudent::MyStudent(int age, char*name, char *teacherName):age(age)
{
cout << "無參建構函式" << this << endl;
int len = strlen(name);
this->name = (char *)malloc(len + 1);
strcpy(this->name, name);
len = strlen(teacherName);
this->teacherName = (char *)malloc(len + 1);
strcpy(this->teacherName, teacherName);
}
MyStudent::~MyStudent()
{
cout << "虛構函式" << this << endl;
free(this->name);
free(this->teacherName);
}
//預設拷貝建構函式 淺拷貝
MyStudent::MyStudent(const MyStudent &student) {
cout << "拷貝建構函式" << this << endl;
this->age = student.age;
this->name = student.name;
this->teacherName = student.teacherName;
}
解決淺拷貝可能存在的問題 在拷貝函式重新申請空間 就可以了
MyStudent::MyStudent(const MyStudent &student) {
cout << "拷貝建構函式" << this << endl;
this->age = student.age;
//this->name = student.name;
int len = strlen(student.name);
this->name = (char *)malloc(len + 1);
strcpy(this->name, student.name);
//this->teacherName = student.teacherName;
len = strlen(student.teacherName);
this->name = (char *)malloc(len + 1);
strcpy(this->name, student.teacherName);
}