Day29.C++03
001.面向对象设计案例_立方体案例
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Cube
{
public:
void setLong(int l)
{
Long = l;
}
int getLong()
{
return Long;
}
void setWidth(int w)
{
Width = w;
}
int getWidth()
{
return Width;
}
void setHeight(int h)
{
Height = h;
}
int getHeight()
{
return Height;
}
void getCubeS()
{
int s = 2 * Long * Width + 2 * Long * Height + 2 * Width * Height;
cout << "立方体的面积为: " << s << endl;
}
void getCubeV()
{
int v = Long * Width * Height;
cout << "立方体的体积为: " << v << endl;
}
bool compareCubeByClass(Cube& cub)
{
bool ret = Long == cub.getLong() && Width == cub.getWidth() && Height == cub.getHeight();
return ret;
}
private:
int Long;
int Width;
int Height;
};
bool compareCube(Cube& c1, Cube& c2)
{
if (c1.getLong() == c2.getLong() && c1.getWidth() == c2.getWidth() && c1.getHeight() == c2.getHeight())
{
return true;
}
else
{
return false;
}
}
void test01()
{
Cube c1;
Cube c2;
c1.setLong(10);
c1.setHeight(10);
c1.setWidth(10);
c2.setLong(10);
c2.setHeight(11);
c2.setWidth(10);
c2.getCubeS();
c2.getCubeV();
bool ret1 = c1.compareCubeByClass(c2);
if (ret1)
{
cout << "通过成员函数判断:两个立方体相等!" << endl;
}
if (!ret1)
{
cout << "通过成员函数判断:两个立方体不相等!" << endl;
}
bool ret2 = compareCube(c1, c2);
if (ret2)
{
cout << "通过全局判断:两个立方体相等!" << endl;
}
if (!ret2)
{
cout << "通过全局判断:两个立方体不相等!" << endl;
}
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
002.面向对象设计案例_点和圆位置关系
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Point {
public:
void setX(int x)
{
m_X = x;
}
int getX()
{
return m_X;
}
void setY(int y)
{
m_Y = y;
}
int getY()
{
return m_Y;
}
private:
int m_X;
int m_Y;
};
class Circle {
public:
void setR(int r)
{
R = r;
}
int getR()
{
return R;
}
void setCenter(Point& p)
{
Center = p;
}
Point getCenter()
{
return Center;
}
void isInCircleByClass(Point& p)
{
int distance = (Center.getX() - p.getX()) * (Center.getX() - p.getX()) + (Center.getY() - p.getY()) * (Center.getY() - p.getY());
int r_2 = R * R;
if (distance > r_2)
{
cout << "利用成员函数判断:点在圆的外部" << endl;
}
else if (distance == r_2)
{
cout << "利用成员函数判断:点在圆上" << endl;
}
else
{
cout << "利用成员函数判断:点在圆的内部" << endl;
}
}
private:
int R;
Point Center;
};
void isInCircle(Circle& c, Point& p)
{
int distance = (c.getCenter().getX() - p.getX()) * (c.getCenter().getX() - p.getX()) + (c.getCenter().getY() - p.getY()) * (c.getCenter().getY() - p.getY());
int r_2 = c.getR() * c.getR();
if (distance > r_2)
{
cout << "利用全局函数判断:点在圆的外部" << endl;
}
else if (distance == r_2)
{
cout << "利用全局函数判断:点在圆上" << endl;
}
else
{
cout << "利用全局函数判断:点在圆的内部" << endl;
}
}
void test01()
{
Point p1;
p1.setX(10);
p1.setY(8);
Circle c1;
Point center;
center.setX(10);
center.setY(0);
c1.setR(10);
c1.setCenter(center);
isInCircle(c1, p1);
c1.isInCircleByClass(p1);
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
003.初始化和清理
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "调用构造函数" << endl;
}
~Person()
{
cout << "调用析构函数" << endl;
}
};
void test01()
{
Person p1;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
004.构造函数的分类和调用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "默认构造函数调用" << endl;
}
Person(int a)
{
cout << "有参构造函数调用" << endl;
}
Person(const Person& p)
{
m_Age = p.m_Age;
cout << "拷贝构造函数调用" << endl;
}
~Person()
{
cout << "析构函数调用" << endl;
}
int m_Age = 0;
};
void test01()
{
Person p1(1);
p1.m_Age = 10;
Person p2(p1);
cout << "p2的年龄: " << p2.m_Age << "岁" << endl;
Person p3;
Person p4 = Person(100);
Person p5 = Person(p4);
Person p6 = Person(p5);
Person p7 = 100;
Person p8 = p7;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
005.拷贝构造函数的调用时机
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "默认构造函数调用" << endl;
}
Person(int a)
{
cout << "有参构造函数调用" << endl;
}
Person(const Person& p)
{
m_Age = p.m_Age;
cout << "拷贝构造函数调用" << endl;
}
~Person()
{
cout << "析构函数调用" << endl;
}
int m_Age;
};
void test01()
{
Person p1;
p1.m_Age = 10;
Person p2(p1);
}
void doWork(Person p1)
{
}
void test02()
{
Person p;
p.m_Age = 10;
doWork(p);
}
Person doWork2()
{
Person p1;
p1.m_Age = 10;
return p1;
}
void test03()
{
Person p1 = doWork2();
}
int main(void)
{
test03();
system("pause");
return EXIT_SUCCESS;
}
006.构造函数的调用规则
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class myClass
{
public:
myClass(int a)
{
cout << "有参构造函数调用" << endl;
}
int m_A;
};
void test01()
{
myClass c1(1);
c1.m_A = 10;
myClass c2(c1);
}
class myClass2
{
public:
myClass2(const myClass& c)
{
m_A = c.m_A;
cout << "拷贝构造函数调用" << endl;
}
int m_A;
};
void test02()
{
}
int main(void)
{
test02();
system("pause");
return EXIT_SUCCESS;
}
007.深拷贝和浅拷贝
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person(){}
Person(const char* name,int age)
{
m_Name = (char*)malloc(strlen(name) + 1);
strcpy(m_Name, name);
m_Age = age;
}
Person(const Person& p)
{
m_Name = (char*)malloc(strlen(p.m_Name) + 1);
strcpy(m_Name, p.m_Name);
m_Age = p.m_Age;
}
~Person()
{
if (m_Name != NULL)
{
free(m_Name);
m_Name = NULL;
}
}
char* m_Name;
int m_Age;
};
void test01()
{
Person p1("xujie", 23);
Person p2(p1);
cout << "p2的姓名:" << p2.m_Name << " p2的年龄:" << p2.m_Age << endl;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
008.初始化列表的基本使用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person():m_A(10),m_B(20),m_C(30) {}
Person(int a, int b, int c):m_A(a),m_B(b),m_C(c)
{}
int m_A;
int m_B;
int m_C;
};
void test01()
{
Person p1(10, 20, 30);
cout << "p1的m_A为: " << p1.m_A << endl;
cout << "p1的m_B为: " << p1.m_B << endl;
cout << "p1的m_C为: " << p1.m_C << endl;
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
009.类对象作为成员
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
using namespace std;
class Phone
{
public:
Phone(){
cout << "手机的默认构造函数调用" << endl;
}
Phone(string name) :phoneName(name){
cout << "手机的有参构造函数调用" << endl;
}
~Phone()
{
cout << "手机的析构函数调用" << endl;
}
string phoneName;
};
class Game
{
public:
Game() {
cout << "游戏的默认构造函数调用" << endl;
}
Game(string name) :gameName(name) {
cout << "游戏的有参构造函数调用" << endl;
}
~Game()
{
cout << "游戏的析构函数调用" << endl;
}
string gameName;
};
class Person
{
public:
Person()
{
cout << "人的默认构造函数调用" << endl;
}
Person(string name, string phoneName, string gameName) :personName(name),personsPhone(phoneName),personsGame(gameName)
{
cout << "人的有参构造函数调用" << endl;
}
~Person()
{
cout << "人的析构函数调用" << endl;
}
void playGame()
{
cout << personName << "用" << personsPhone.phoneName << "手机玩" << personsGame.gameName << "游戏!" << endl;
}
string personName;
Phone personsPhone;
Game personsGame;
};
void test01()
{
Person p1("徐杰", "小米", "王者荣耀");
p1.playGame();
}
int main(void)
{
test01();
system("pause");
return EXIT_SUCCESS;
}
010.explicit关键字
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class myString
{
public:
myString() {}
myString(const char* str)
{
mStr = (char*)malloc(strlen(str) + 1);
strcpy(mStr, str);
}
explicit myString(int a)
{
mSize = a;
}
~myString()
{
if (mStr != NULL)
{
free(mStr);
mStr = NULL;
}
}
char* mStr;
int mSize;
};
void test01()
{
myString str = "abc";
myString str2(10);
}
int main(void)
{
system("pause");
return EXIT_SUCCESS;
}
011.new运算符使用
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
class Person
{
public:
Person()
{
cout << "默认函数调用" << endl;
}
Person(int a)
{
cout << "有参函数调用" << endl;
}
~Person()
{
cout << "析构函数调用" << endl;
}
};
void test01()
{
Person p1;
Person* p2 = new Person;
delete p2;
}
void test02()
{
void* p = new Person;
delete p;
}
void test03()
{
Person* pArray = new Person[10];
Person pArray2[2] = { Person(1),Person(2) };
delete [] pArray;
}
int main(void)
{
test03();
system("pause");
return EXIT_SUCCESS;
}