类与对象
内存分区
代码区(共享、只读)
程序运行前;存放函数体的二进制代码(机器指令)
全局区
存放全局变量和静态变量以及常量(包括const修饰的全局变量)
栈区
局部变量;形参数据;
不要返回局部变量的地址
堆区
由程序员管理,c++中主要利用new开辟,delete释放;c语言中使用malloc动态分配,
free释放;
类中的数组成员赋值
不要返回局部或者临时对象的引用
成员属性声明加关键字
mutable
:在常量函数中依然可以修改
引用
引用必须初始化
初始化之后不能再改变
不能返回局部变量的引用
如果函数的返回值是引用,这个函数调用可以
作为左值
常量引用
常量
字符串常量
(
全局区
)
const
修饰的常量
const
修饰的全局变量
(
全局区
)
const
修饰的局部变量(栈区)
int a;//必须初始化
int &b=a;//初始化之后不能再改变
int &test(void)//不能返回局部变量的引用
{
int a=10;
return a;
}
#include<iostream>
#include<string>
using namespace std;
int& test(void)
{
static int a = 20;
return a;
}
int main(void)
{
int& ref = test();
test() = 100;
cout << ref << endl;
return 0;
}
//
不管原值和引用都是等于同一个地址中的数据,其中一个改变
//
其他的都会跟着改变
//
引用的本质是一个指针常量
int a ;int & ref = a ; // 自动转化为指针常量int * const ref =& a ; 用来修饰形参,防止误操作
类和对象
属性(变量)+行为(函数)
public
公共权限
类内可以访问 类外可以访问
protected 保护权限 类内可以访问 类外不可以访问 子类可以访问父类的保护内容
private
私有权限
类内可以访问 类外不可以访问
使用struct定义类 默认权限为公共
使用class定义类 默认权限为私有
#include<iostream>
#include<string>
using namespace std;
double PI = 3.14;
class circle
{
public://共有部分
int m_r;//圆的半径
double calculate(void)//计算周长的函数
{
return 2 * PI * m_r;
}
};
int main(void)
{
circle c1;//实例化类的对象
c1.m_r = 10;//给定圆的周长
cout << "圆的周长" << c1.calculate() << endl;//输出圆的周长
return 0;
}
class circle
{
public:
string name;
int data;
void calculate(void)//函数打印学生的信息
{
cout << "学生的姓名:" << name << endl;
cout << "学生的学号:" << data << endl;
}
};
int main(void)
{
circle c1;
c1.name= "chen";
c1.data = 181101;
c1.calculate();
return 0;
}
//
长方体类
#include<iostream>
#include<string>
using namespace std;
class Cube
{
private:
int m_l;
int m_w;
int m_h;
public:
void setL(int l)
{
m_l = l;
}
int getl(void)
{
return m_l;
}
void setw(int w)
{
m_w = w;
}
int getw(void)
{
return m_w;
}
void seth(int h)
{
m_h = h;
}
int geth(void)
{
return m_h;
}
int calculates(void)
{
return 2 * (m_l * m_w + m_l * m_h + m_h * m_w);
}
int calculatev(void)
{
return m_l * m_w * m_h;
}
//
利用行为判断是否相等
bool iss ( Cube & c1 ){if ( m_l == c1 . getl () && m_w == c1 . getw () && m_h == c1 . geth ())return 1 ;return 0 ;
构造函数&析构函数
构造函数->没有返回值也不写void;函数名称与类名相同;构造函数可以有参数,可以发
生重载;程序在调用对象时候会自动调用构造,无须手动调用,而且只会调用一次;自己没
有定义构造函数,c++会提供默认的构造函数;如果自己定义了构造函数,c++则不再提供
默认的构造函数,可以自己定义默认的构造函数;
}
};
//
利用全局函数判断
bool issame(Cube& c1, Cube& c2)
{
if (c1.getl() == c2.getl() && c1.getw() == c2.getw() && c1.geth()
== c2.geth())
return 1;
return 0;
}
int main(void)
{
Cube a,b;
a.setw(5);
a.setL(9);
a.seth(2);
b.setw(5);
b.setL(9);
b.seth(2);
int i = issame(a, b);
if (i)
cout << "两个长方体是相等的" << endl;
else
cout << "两个长方体是不相等的"<<endl;
if(a.iss(b))
cout << "两个长方体是相等的" << endl;
else
cout << "两个长方体是不相等的" << endl;
cout <<"长方体的体积为" << a.calculatev() << endl;
cout<<"长方体的面积为"<<a.calculates() << endl;
return 0;
}
//
成员函数需要加上函数的作用域
//
一个类可以作为另一个类的成员
stock::stock(const string &co,long n,double pr);
默认构造函数所有的值都有默认值
构造函数有成员不是默认值(需要主动传递参数)
析构函数->没有返回值也不写void;函数名称与类名相同,在名称前面加上符号~;析构函
数可以没有参数,不可以发生重载;程序在销毁对象时候会自动调用析构,无须手动调用,
而且只会调用一次;
this->用来指向调用成员函数的对象
指针常量
定义的常对象只能调用常函数;因为普通函数可以修改类的属性
对象数组
类的声明和成员函数的定义里不需要加(::stock)
stock::~stock()
{
}//析构函数没有参数,没有返回值
//入栈
arr[a++]=data;
//出栈
data=arr[--a];
#include"main.h"
using namespace std;
int main(void)
{
bankaccount a("chen","zhongguoyinhang",100);
a.deposit(12.0);
a.show();
a.withdraw(8.2);
a.show();
return 0;
}
#ifndef __MAIN_H__
#define __MAIN_H__
#include<string>
#include<iostream>
using namespace std;
class bankaccount
{
private:
string name;
string accountnum;
double balance;
public:
bankaccount(string client,string num,double bal=0.0);
void show(void)const;//不希望修改成员变量
void deposit(double cash);
void withdraw(double cash);
};
#endif
#include"main.h"重载运算
例如:operator+()重载+运算符
类中的重载运算:operator+()成员函数
using namespace std;
bankaccount::bankaccount(string client, string num, double bal)//默认参
数是申明的时候写,定义的时候不写
{
name = client;
accountnum = num;
balance = bal;
cout << "初始化时账户的余额为 " << balance << "人民币" << endl;
}
void bankaccount::show(void)const
{
cout << "账号的姓名 " << name << endl;
cout << "账户的账号 " << accountnum << endl;
cout << "账户的余额 " << balance << endl;
}
void bankaccount::deposit(double cash)
{
balance += cash;
cout << "存入" << cash << "人民币" << endl;
}
void bankaccount::withdraw(double cash)
{
balance -= cash;
cout << "取出" << cash << "人民币" << endl;
}
//未使用重载运算
#include"main.h"
using namespace std;
int main(void)
{
Time a(2, 40);
Time b(3, 20);
Time sum;
a.show();
b.show();
sum = a.sumss(b);
sum.show();
return 0;
}
#ifndef __MAIN_H__
#define __MAIN_H__
#include<string>
#include<iostream>
#include<cstring>
using namespace std;class Time
{
private:
int hours;
int minutes;
public:
Time();
Time(int h, int m);
void addmin(int m);
void addhr(int h);
void reset(int h = 0, int m = 0);
Time sumss(const Time& t)const;//引用
void show(void)const;
};
#endif
#include"main.h"
Time::Time()
{
hours = minutes = 0;
}
Time::Time(int h, int m)
{
hours = h;
minutes = m;
}
void Time::addmin(int m)
{
minutes += m;
hours += minutes / 60;
minutes %= 60;
}
void Time::addhr(int h)
{
hours += h;
}
void Time::reset(int h, int m)
{
hours = h;
minutes = m;
}
Time Time::sumss(const Time& t)const//引用转递速度更快
{
Time sum;//局部变量,不能返回局部的引用
sum.minutes = minutes + t.minutes;
sum.hours = hours + t.hours + sum.minutes / 60;
sum.minutes %= 60;
return sum;
}
void Time::show()const
{
cout << hours << "hours; " << minutes << "minutes:" << endl;
}
友元
友元的三种实现方法:
全局函数做友元
类做友元
在想访问类里面加入语句
friend
全局函数;
#include"main.h"
building::building()
{
m_SittingRoom = "客厅";
m_bedroom = "卧室";
}
goodgay::goodgay()
{
build = new building;
}
void goodgay::visit()
{
cout << "好基友正在访问: " << build->m_SittingRoom << endl;//共有
客厅
cout << "好基友正在访问: " << build->m_bedroom << endl;//私有 卧室
}
int main(void)
{
goodgay a;
building b;
a.visit();
}
#ifndef __MAIN_H__
#define __MAIN_H__
#include<iostream>
#include<string>
using namespace
std
;
class
building
;
class
goodgay
{
public
:
goodgay
();
void
visit
();
building
*
build
;
};
class
building
{
friend class
goodgay
;
//***
注意 重要代码
public
:
building
();
成员函数做友元
类型转换
隐式类型转换
显式类型转换
类与标准类型间的转换
1.转换构造函数进行类型转换
将构造函数用在自动类型转换
构造函数前加上关键字exolicit可以关闭这种功能
2.类型转换函数 operator typeNane();
operator 目标类型()
{
函数体;
}
转换函数必须是类方法
转换函数不能指定返回类型
转换函数不能有参数
显式转换:int(poppins);
隐式转换:int p=poppins;
类和动态内存分配
构造函数申请内存,析构函数释放内存
构造函数
默认构造函数
string m_SittingRoom
;
private
:
string m_bedroom
;
};
#endif
// !__MAIN_H__
int
a
=
5
,
b
;
b
=
3.5
+
a
;
double
i
=
2.2
,
j
=
3.3
;
cout
<<
int
(
i
+
j
);
自定义构造函数
(新)复制构造函数
(新)复制构造函数
//
用于初始化过程,按值传递时调用复制构造函数
//
返回值为类的对象时,调用复制函数
//
写法:
stringbad::stringbad
(
const
stringbad
&
st
)
//
形参为一个类
d