类的实例化
#include <iostream>
#include "Circle.h"
using namespace std;
int main()
{
// 对象的两种实例化
// 第一种:栈中实例化。这种实例对象使用完,自动销毁内存。
Circle c;
c.setR(100);
cout << "c面积是:" << c.setArea() << endl;
cout << "c圆周是:" << c.setGirth() << endl;
Circle cs[2];
for (int i = 0; i < 2; i++)
{
cs[i].setR(100 + i);
cout << "*cs[" << i << "]面积是:" << cs[i].setArea() << endl;
cout << "*cs[" << i << "]圆周是:" << cs[i].setGirth() << endl;
}
cout << "----------------------" << endl;
// 第二种:堆中实例化。这种实例对象必须手动销毁。
Circle *cp = new Circle();
if (NULL == cp) {
return 0;
}
cp->setR(100);
cout << "*cp面积是:" << cp->setArea() << endl;
cout << "*cp圆周是:" << cp->setGirth() << endl;
delete cp;
cp = NULL;
Circle *cps = new Circle[2];
if (NULL == cps) {
return 0;
}
for (int i = 0; i < 2; i++)
{
cps[i].setR(100 + i);
cout << "*cps["<< i <<"]面积是:" << cps[i].setArea() << endl;
cout << "*cps[" << i << "]圆周是:" << cps[i].setGirth() << endl;
}
delete[]cps;
cps = NULL;
#if 0
c面积是:31400
c圆周是:628
*cs[0]面积是:31400
*cs[0]圆周是:628
*cs[1]面积是:32031.1
*cs[1]圆周是:634.28
----------------------
*cp面积是:31400
*cp圆周是:628
*cps[0]面积是:31400
*cps[0]圆周是:628
*cps[1]面积是:32031.1
*cps[1]圆周是:634.28
#endif
system("pause");
return 0;
}
类的封装与类外定义
Student.h
#pragma once
#include <string>
using namespace std;
class Student
{
public:
Student();
string getName();
void setName(string _name);
int getAge();
void setAge(int _age);
~Student();
private:
string m_strName;
int m_iAge;
};
#include "Student.h"
#include <iostream>
#include <string>
using namespace std;
Student::Student()
{
}
string Student::getName()
{
return m_strName;
}
void Student::setName(string _name)
{
m_strName = _name;
}
int Student::getAge()
{
return m_iAge;
}
void Student::setAge(int _age)
{
if (_age < 100 && _age > 0)
{
m_iAge = _age;
}
else {
m_iAge = -1;
}
}
Student::~Student()
{
}
main.cpp
int main()
{
Student stu;
stu.setAge(60);
cout << "student age is " << stu.getAge() << endl;
#if 0
student age is 60
#endif
system("pause");
return 0;
}
内存分区
- 栈区:int x = 0; int* p = NULL;
- 堆区:int *p = new int[20];
- 全局区:储存全局变量与静态变量
- 常量区:string str = "hello world";
- 代码区:储存逻辑代码的二进制
如图:
构造函数初始化列表
Student.h
class Student
{
public:
Student(string name="alex", int age=12);
...
Student.cpp
// 初始化列表特点
// 1.初始化列表先于构造函数执行
// 2.初始化列表只能用于构造函数
// 3.初始化列表可以同时初始化多个数据成员
Student::Student(string name, int age):m_strName(name),m_iAge(age) {
cout << name << " " << age << endl;
}
拷贝构造函数
格式:类名(const 类名& 变量名 )
例子:
// 拷贝构造函数
Student(const Student& stu) {
}
- 如果没有定义拷贝构造函数则系统默认自动生成一个拷贝构造函数
- 当采用直接初始化或复制初始化实例化对象时系统自动调用拷贝构造函数
对象成员
习题:对象成员
要求:定义两个类:
坐标类:Coordinate
数据成员:横坐标m_iX,纵坐标m_iY
成员函数:构造函数、析构函数、数据封装函数
线段类:Line
数据成员:点A m_coorA,点B m_coorB
成员函数:构造函数、析构函数、数据封装函数
Coordinate.h
#pragma once
class Coordinate
{
public:
Coordinate(int x, int y);
~Coordinate();
int getX();
void setX(int x);
int getY();
void setY(int y);
private:
int m_iX;
int m_iY;
};
Coordinate.cpp
#include "Coordinate.h"
#include <iostream>
using namespace std;
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
cout << "Coordinate()" << m_iX << ", " << m_iY << endl;
}
Coordinate::~Coordinate()
{
cout << "~Coordinate()" << endl;
}
int Coordinate::getX()
{
return m_iX;
}
void Coordinate::setX(int x)
{
m_iX = x;
}
int Coordinate::getY()
{
return m_iY;
}
void Coordinate::setY(int y)
{
m_iY = y;
}
Line.h
#pragma once
#include "Coordinate.h"
class Line
{
public:
Line(int x1, int y1, int x2, int y2);
~Line();
Coordinate getA();
void setA(int x, int y);
Coordinate getB();
void setB(int x, int y);
void printinfo();
private:
Coordinate m_coorA;
Coordinate m_coorB;
};
Line.cpp
#include "Line.h"
#include <iostream>
using namespace std;
Line::Line(int x1, int y1, int x2, int y2):m_coorA(x1, y1),m_coorB(x2, y2)
{
cout << "Line()" << endl;
}
Line::~Line()
{
cout << "~Line()" << endl;
}
Coordinate Line::getA()
{
return m_coorA;
}
void Line::setA(int x, int y)
{
m_coorA.setX(x);
m_coorA.setY(y);
}
Coordinate Line::getB()
{
return m_coorB;
}
void Line::setB(int x, int y)
{
m_coorB.setX(x);
m_coorB.setY(y);
}
void Line::printinfo()
{
cout << m_coorA.getX() << ", " << m_coorA.getY() << ", " << m_coorB.getX() << ", " << m_coorB.getY() << endl;
}
main.cpp
#include <iostream>
#include "Line.h"
using namespace std;
int main()
{
Line *line = new Line(1,2,3,4);
line->printinfo();
delete line;
line = NULL;
#if 0
Coordinate()1, 2
Coordinate()3, 4
Line()
1, 2, 3, 4
~Line()
~Coordinate()
~Coordinate()
#endif
system("pause");
return 0;
}
浅拷贝
Array.h
#pragma once
class Array
{
public:
Array();
Array(const Array& arr);
~Array();
void setCount(int count);
int getCount();
private:
int m_iCount;
};
Array.cpp
#include "Array.h"
#include <iostream>
using namespace std;
// 浅拷贝
Array::Array()
{
cout << "Array()" << endl;
}
Array::Array(const Array& arr)
{
m_iCount = arr.m_iCount;
cout << "Array() & " << endl;
}
Array::~Array()
{
cout << "~Array()" << endl;
}
void Array::setCount(int count)
{
m_iCount = count;
}
int Array::getCount()
{
return m_iCount;
}
main.cpp
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
Array arr1;
arr1.setCount(10);
Array arr2(arr1);
cout << "arr2.m_iCount: " << arr2.getCount() << endl;
#if 0
Array()
Array() &
arr2.m_iCount: 10
#endif
system("pause");
return 0;
}
深拷贝
Array.h
#pragma once
class Array
{
public:
Array(int count);
Array(const Array& arr);
~Array();
void setCount(int count);
int getCount();
void printinfo();
private:
int m_iCount;
int *m_pAddr;
};
Array.cpp
#include "Array.h"
#include <iostream>
using namespace std;
// 深拷贝
Array::Array(int count)
{
m_iCount = count;
m_pAddr = new int[m_iCount];
cout << "Array()" << endl;
}
Array::Array(const Array& arr)
{
m_iCount = arr.m_iCount;
m_pAddr = new int[m_iCount];
for (int i = 0; i < m_iCount; i++)
{
m_pAddr[i] = arr.m_pAddr[i];
}
cout << "Array() & " << endl;
}
Array::~Array()
{
delete[] m_pAddr;
m_pAddr = NULL;
cout << "~Array()" << endl;
}
void Array::setCount(int count)
{
m_iCount = count;
}
int Array::getCount()
{
return m_iCount;
}
void Array::printinfo()
{
cout << "m_pAddr的值是:" << m_pAddr << endl;
}
main.cpp
#include <iostream>
#include "Array.h"
using namespace std;
int main()
{
Array arr1(5);
Array arr2(arr1);
arr1.printinfo();
arr2.printinfo();
#if 0
Array()
Array() &
m_pAddr的值是:00537520
m_pAddr的值是:00537560
#endif
system("pause");
return 0;
}
对象指针
练习:
定义Coordinate类
数据成员:m_iX和m_iY
声明对象指针,并通过指针操控对象
计算两个点,横、纵坐标的和
数据成员:m_iX和m_iY
声明对象指针,并通过指针操控对象
计算两个点,横、纵坐标的和
Coordinate.h
#pragma once
class Coordinate
{
public:
Coordinate();
~Coordinate();
int getX();
void setX(int x);
int getY();
void setY(int y);
void summarize();
private:
int m_iX;
int m_iY;
};
Coordinate.cpp
#include "Coordinate.h"
#include <iostream>
using namespace std;
Coordinate::Coordinate()
{
cout << "Coordinate()" << endl;
}
Coordinate::~Coordinate()
{
cout << "~Coordinate()" << endl;
}
int Coordinate::getX()
{
return m_iX;
}
void Coordinate::setX(int x)
{
m_iX = x;
}
int Coordinate::getY()
{
return m_iY;
}
void Coordinate::setY(int y)
{
m_iY = y;
}
void Coordinate::summarize()
{
int sum = m_iX + m_iY;
cout << "m_iX + m_iY = " << sum << endl;
}
main.cpp
#include <iostream>
using namespace std;
/*
要求:
定义Coordinate类
数据成员:m_iX和m_iY
声明对象指针,并通过指针操控对象
计算两个点,横、纵坐标的和
*/
#include "Coordinate.h"
int main()
{
// 堆中实例化对象
Coordinate *p1 = new Coordinate; // 默认构造函数,有也可以没有 括号
p1->setX(10); // 指针赋值方式
(*p1).setY(2); // 对象赋值方式
Coordinate *p2 = NULL;
p2 = new Coordinate();
p2->setX(20);
(*p2).setY(30);
cout << "p1.X + p2.X = " << p1->getX() + p2->getX() << endl;
cout << "p2.X + p2.Y = " << p1->getY() + p2->getY() << endl;
delete p2;
p2 = NULL;
delete p1;
p1 = NULL;
#if 0
Coordinate()
Coordinate()
p1.X + p2.X = 30
p2.X + p2.Y = 32
~Coordinate()
~Coordinate()
#endif
system("pause");
return 0;
}
成员对象指针
练习:
成员对象指针
定义两个类:
坐标类:Coordinate
数据成员:横坐标m_iX,纵坐标m_iY
成员函数:构造函数、析构函数、数据封装函数
线段类:Line
数据成员:点A指针 m_pCoorA,点B指针m_pCoorB
成员函数:构造函数、析构函数、信息打印函数
定义两个类:
坐标类:Coordinate
数据成员:横坐标m_iX,纵坐标m_iY
成员函数:构造函数、析构函数、数据封装函数
线段类:Line
数据成员:点A指针 m_pCoorA,点B指针m_pCoorB
成员函数:构造函数、析构函数、信息打印函数
Coordinate.h
#pragma once
class Coordinate
{
public:
Coordinate(int x, int y);
~Coordinate();
int getX();
void setX(int x);
int getY();
void setY(int y);
private:
int m_iX;
int m_iY;
};
Coordinate.cpp
#include "Coordinate.h"
#include <iostream>
using namespace std;
Coordinate::Coordinate(int x, int y)
{
m_iX = x;
m_iY = y;
cout << "Coordinate() " << m_iX << "," << m_iY << endl;
}
Coordinate::~Coordinate()
{
cout << "~Coordinate() " << m_iX << "," << m_iY << endl;
}
int Coordinate::getX()
{
return m_iX;
}
void Coordinate::setX(int x)
{
m_iX = x;
}
int Coordinate::getY()
{
return m_iY;
}
void Coordinate::setY(int y)
{
m_iY = y;
}
Line.h
#pragma once
#include "Coordinate.h"
class Line
{
public:
Line(int x1, int y1, int x2, int y2);
~Line();
void printinfo();
private:
Coordinate *m_pCoorA;
Coordinate *m_pCoorB;
};
Line.cpp
#include "Line.h"
#include <iostream>
using namespace std;
Line::Line(int x1, int y1, int x2, int y2)
{
m_pCoorA = new Coordinate(x1, y1);
m_pCoorB = new Coordinate(x2, y2);
cout << "Line()" << endl;
}
Line::~Line()
{
delete m_pCoorA;
m_pCoorA = NULL;
delete m_pCoorB;
m_pCoorB = NULL;
cout << "~Line()" << endl;
}
void Line::printinfo()
{
cout << "printinfo()" << endl;
cout << "(" << m_pCoorA->getX() << ", " << m_pCoorA->getY() << ")" << endl;
cout << "(" << m_pCoorB->getX() << ", " << m_pCoorB->getY() << ")" << endl;
}
main.cpp
#include <iostream>
#include "Line.h"
using namespace std;
/*
成员对象指针
定义两个类:
坐标类:Coordinate
数据成员:横坐标m_iX,纵坐标m_iY
成员函数:构造函数、析构函数、数据封装函数
线段类:Line
数据成员:点A指针 m_pCoorA,点B指针m_pCoorB
成员函数:构造函数、析构函数、信息打印函数
*/
int main()
{
Line *p = new Line(1, 2, 3, 4);
p->printinfo();
delete p;
p = NULL;
#if 0
Coordinate() 1, 2
Coordinate() 3, 4
Line()
printinfo()
(1, 2)
(3, 4)
~Coordinate() 1, 2
~Coordinate() 3, 4
~Line()
#endif
system("pause");
return 0;
}
this指针
练习:
定义一个Array类
数据成员:m_iLen表示数组长度
成员函数:
构造函数
析构函数
len的封装函数
信息输出函数printInfo
数据成员:m_iLen表示数组长度
成员函数:
构造函数
析构函数
len的封装函数
信息输出函数printInfo
#pragma once
class Array
{
public:
Array(int len);
~Array();
int geLen();
Array* setLen(int len);
Array* printInfo();
private:
int len;
};
Array.cpp
#include "Array.h"
#include <iostream>
using namespace std;
Array::Array(int len)
{
this->len = len;
cout << "Array()" << endl;
}
Array::~Array()
{
cout << "~Array()" << endl;
}
int Array::geLen()
{
return this->len;
}
Array* Array::setLen(int len)
{
this->len = len;
return this;
}
Array* Array::printInfo()
{
cout << "长度为" << this->len << endl;
return this;
}
main.cpp
#include <iostream>
#include "Array.h"
using namespace std;
/*
定义一个Array类
数据成员:m_iLen表示数组长度
成员函数:
构造函数
析构函数
len的封装函数
信息输出函数printInfo
*/
int main()
{
Array arr1(10);
arr1.printInfo()->setLen(15)->printInfo();
#if 0
Array()
长度为10
长度为15
#endif
system("pause");
return 0;
}
常指针和常引用
练习:
定义一个坐标类,在栈上实例化坐标类常对象,并给出坐标(3,5),然后定义常引用、常指针,最后使用对象、引用、指针分别通过调用信息打印函数打印坐标信息。
打印结果如下:
打印结果如下:
(3,5)
(3,5)
(3,5)
(3,5)
(3,5)
#include <iostream>
using namespace std;
class Coordinate
{
public:
Coordinate(int x, int y)
{
// 设置X,Y的坐标
m_iX = x;
m_iY = y;
}
// 实现常成员函数
void printInfo() const
{
cout << "(" << m_iX << "," << m_iY <<")" <<endl;
}
public:
int m_iX;
int m_iY;
};
int main(void)
{
const Coordinate coor(3, 5);
// 创建常指针p
const Coordinate *p = &coor;
// 创建常引用c
const Coordinate &c = coor;
coor.printInfo();
p->printInfo();
c.printInfo();
return 0;
}