一、C++ 概念
编译工具 g++ 源文件扩展名cpp
#include <iostream>
using namespace std;
#include <stdio.h>
int main(int argc, char *argv[])
{
cout << "hello world\n";
printf("open\n");
}
二、C / C++的差异性
C语言是面向过 程编程,C++是面向对象编程。
2.1、相对于C/ C++ 有更为严格的类型检查
2.2、常变量 const只读变量
C语言中 ,const修饰的变量的数值、不可以该变量改变、但是可以通过非 const指针修改。
此处:&a 的数据类型为 const int *
c++中,const 修饰一个变量,表示该变量永远只读,只能使用,不能修改const 修饰的变量为只读变量
错误:c++ 有更为严格的类型检查,不能将 const int * 转换成 int *
#include <stdio.h>
int main()
{
const int a = 13;
int *p = (int *)&a;
*p += 3;
printf("a: %d\n", a);
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
const int a = 13;
int *p = (int *)&a;
*p += 3;
cout << "a: " << a << endl;
}
2.3、强制类型转换
int b = int(a); //c++中的类型强制转换
int c = int(a + b); //c++中的类型强制转换
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
double a = 13.1111;
int b = int(a);
int c = int(a + b);
}
2.4、变量的引用
引用:就是为已存在的变量,取别名,是一种新的数据类型。
注意:
1、取别名时,必须赋值,但不能为常量取别名。
2、数据类型 & 变量名 = 变量;
3、函数参数传参也可以使用 引用
4、在函数被调用时,才会启用形参取别名
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int a = 13;
int &b = a;
cout << "&b: " << &b << endl;
cout << "&a: " << &a << endl;
b += 2;
cout << "&a: " << a << endl;
int c = 11;
swap(a, c);
cout << "a: " << a << "c: " << c << endl;
}
void swap(int &x, int &y)
{
int tmp = x;
x = y;
y = tmp;
}
2.5、函数重载
对一个函数名重新赋予它的新含义,使一个函数名可以多用。
c++中的函数重载,函数名相同,但参数列表必须不同
1、参数个数不同
2、参数个数相同、类型不同
使用的时候看实参来调用
#include <iostream>
using namespace std;
void sum()
{
cout << "18\n";
}
void sum(int a)
{
cout << "23\n";
}
void sum(int a, int b)
{
cout << "28\n";
}
void sum(int a, int b, int c)
{
cout << "33\n";
}
void sum(float a, float b)
{
cout << "38\n";
}
int main(int argc, char *argv[])
{
int a = 13;
sum();
sum(12);
sum(1, 3);
sum(1, 3, 5);
sum(float(1.1), float(1.2));
}
2.6、默认参数
c++的函数可以带默认参数:
默认参数设置规则:
1、只能在函数声明时,设置默认参数,定义时不能写默认值
2、默认参数只能从参数列表的最右开始,依次往左赋值,否则error 中间不能跳跃。
注意:
1、默认参数结合函数重载,在调用时,容易参数歧义(二义性)。
#include <iostream>
using namespace std;
int a;
int add()
{
cout << __LINE__ << endl;
}
int add(int x = 5)
{
cout << __LINE__ << endl;
}
int add(int x, int y, int z = 100)
{
cout << __LINE__ << endl;
}
int add(int x, int y, int &c = a)
{
cout << __LINE__ << endl;
}
int main(int argc, char *argv[])
{
add(1, 2, 3);
add(1, 2);
add(6);
}
2.7、内联函数
内联函数,只在声明时使用 inline 说明。
注意:
1、频繁使用的函数
2、函数代码语句很少(<= 5局)
3、函数代码语句简单,没有控制语句(循环语句,switch)
目的:
1、优点:提升代码运行效率;缺点:目标程序体积较大
#include <iostream>
using namespace std;
inline int add(int a, int b);
int main(int argc, char *argv[])
{
cout << add(1, 2) << endl;
}
int add(int x, int y)
{
return x+y;
}
2.8、结构体
1、c++ 中为了保护数据的安全性,引入了访问权限限定符。
2、结构体变量初始化、引入了构造函数
3、释放结构体变量占用资源、引入了构造函数
#include <iostream>
using namespace std;
struct Demo{
public:
void setValue(int a, int b);
int getX();
int getY()
{
return y;
}
private:
int x, y;
};
void Demo::setValue(int a, int b)
{
x = a;
y = b;
}
int Demo::getX()
{
return x;
}
int main(int argc, char *argv[])
{
struct Demo obj;
obj.setValue(1, 3);
cout << obj.getX() << endl;
cout << obj.getY() << endl;
}
#include <iostream>
using namespace std;
#define pri() cout<<__LINE__<<"func:"<<__func__<<endl;
struct Demo{
public:
void setValue(int a, int b);
int getX();
int getY()
{
return y;
}
Demo()
{
pri();
}
Demo(int x)
{
pri();
}
Demo(int x, int y)
{
this-> x = x;
this-> y = y;
pri();
}
~Demo()
{
pri();
}
private:
int x, y;
};
#if 0
void Demo::setValue(int a, int b)
{
x = a;
y = b;
}
#endif
void Demo::setValue(int x, int y)
{
cout << this << endl;
this-> x = x;
this-> y = y;
}
int Demo::getX()
{
return x;
}
int main(int argc, char *argv[])
{
struct Demo obj;
cout << &obj << endl;
obj.setValue(1, 3);
cout << obj.getX() << endl;
cout << obj.getY() << endl;
struct Demo obj1(2, 5);
cout << obj1.getX() << endl;
cout << obj1.getY() << endl;
}
三、内存模型及名字空间
3.1、作用域
局部域
名字空间域
类域
变量的作用域:
局部变量
全局变量
3.2、链接性
链接性:描述名称如何在各个单元中共享。
外部链接:名称可以在文件间共享
内部链接:名称仅仅能在一个文件中的函数共享。
#include <iostream>
using namespace std;
int a = 100;
int add()
{
return a;
}
#include <iostream>
using namespace std;
int a = 100;
int main(int argc, char *argv[])
{
cout << a << endl;
}
#include <iostream>
using namespace std;
int a = 100;
int add()
{
return a;
}
#include <iostream>
using namespace std;
extern int a;
extern int add();
int b = 13;
int c = 10;
int main(int argc, char *argv[])
{
cout << a << endl;
cout << add() << endl;
int b = 11;
cout << b << endl;
cout << ::a << endl;
extern int c;
cout << c << endl;
c+=1;
cout << c << endl;
cout << ::c << endl;
}
#include <stdio.h>
int add(int x, int y)
{
return x+y;
}
#include <iostream>
using namespace std;
extern "C" int add(int, int);
int main(int argc, char *argv[])
{
cout << add(1, 3) << endl;
}
3.3、动态内存
1、new
在堆区开辟一个空间
2、delete
释放开辟的堆空间
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int *p;
p = new int;
*p = 13;
cout << *p << endl;
int *q = new int(20);
cout << *q << endl;
delete p;
int *buf = new int[5];
buf[0] = 1;
buf[1] = 2;
cout << *(buf) << *(buf + 1) << endl;
delete []buf;
}
3.4、声明与作用域
1、声明区,可以进行声明的区域
namespace MySpace{ //给这片声明区取名字 MySpace
}
2、名字空间
::
3、潜在的作用域
从声明点、到声明区结尾
#include <iostream>
using namespace std;
int a = 10;
int b = 20;
void demo()
{
cout << __LINE__ << endl;
}
namespace MySpace{
int a = 13;
void demo()
{
cout << __LINE__ << endl;
}
}
namespace B{
int b = 22;
void demo()
{
cout << __LINE__ << endl;
}
}
int main(int argc, char *argv[])
{
cout << a << endl;
using MySpace::a;
cout << a << endl;
cout << a << endl;
cout << ::a << endl;
using MySpace::demo;
demo();
::demo();
B::demo();
}
四、输入输出流
4.1、输出流
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "hello Word" << std::endl;
}
1、引用头文件:
#include <iostream>
using namespace std;
2、基本形式如下:
cout << 表达式1 << 表达式2 << ...;
3、换行:
控制符:endl
换行符:\n
4、一个cout语句可以分写若干行
#include <iostream>
using namespace std;
int main (int argc, char *agv[])
{
cout << "hello world" << endl;
cout << "hello"
"world"
<< endl;
return 0;
}
5、cout输出时,系统会自动判别输出数据的类型
#include <iostream>
using namespace std;
int main (int argc, char *agv[])
{
int a 123:
double b 456.789;
char c= 'a';
cout << a << b << c << endl;
return 0;
}
4.1、输入流
1、引用头文件:
#include <iostream>
using namespace std;
2、基本形式如下:
cin >> 变量1 >> 变量2 >> ...;
#include <iostream>
using namespace std;
int main (int argc, char *agv[])
{
int ival;
char ch:
cin >> ival >> ch
cout << ival << ch << endl;
return 0
}
3、换行:
控制符:endl
换行符:\n
4、一个cin语句可以分写若干行
5、cout输入时,系统会自动判别输人数据的类型
4.1、格式化控制符
#include <iostream>
#include <iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int a = 50;
float b = 50.01;
char c = 50;
cout << "a = " << a << "\n";
cout << "b = " << b << endl;
cout << "c = " << c << endl;
cout << showpos << a << endl;
cout << right << setw(8) << a << endl;
cout << left << setw(8) << a << endl;
float d = 50.00;
cout << showpoint << d << endl;
cout << "hello"
"world"
<<endl;
}
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int i = 0;
float f = 0;
char c = 0;
cout << "please input i:" << endl;
cin >> i;
cout << "i = " << i << endl;
cout << "please input f and c:" << endl;
cin >> f >> c;
cout << "f = " << f << endl;
cout << "c = " << c << endl;
cout << "f = " << f << "c = " << c << endl;
}
五、对象和类
5.1、OPP思想
1、抽象:就是声明定义一个类,抽象概括一类对象的公共性质。
数据抽象:设置成员变量
代码抽象:设置成员函数
2、封装:将数据成员和成员函数结合在一起,形成一个整体,就是类体部分。
5.2、类的声明定义
1、类体中默认权限为:private 私有的
2、成员函数在类体之外的定义语法:
数据类型 类名::函数名(<参数列表>){函数体}
3、成员变量的作用域:
整个类的作用域(类域):类的所有成员函数的函数体区域
3、类的所有成员函数都有一个特殊的指针 this,指向 访问当前成员函数的对象
4、类:
代码角度:用户自定义的数据类型,由class 进行说明
抽象角度:对象的类型,是一批对象的共享和特征,是对象的抽象概括
5、对象:
代码角度:就是一个变量
抽象角度:具备行为和属性的事物,一切皆为对象,也就是类的具体实列
#include <iostream>
using namespace std;
class Student;
class Student{
public:
void setValue(char , int );
private:
char sex;
int age;
};
void Student::setValue(char sex, int age)
{
this->sex = sex;
this->age = age;
}
int main(int argc, char *argv[])
{
class Student stu;
stu.setValue(1, 2);
class Student *stu1;
Student stu2;
stu2 = stu;
Student *stu3;
stu1 = &stu;
stu3 = new Student;
stu1->setValue(2, 3);
}
6、如果设计一个类时,没有显示声明会自动生成:
- 构造函数
- 析构函数
- 复制构造函数
- 赋值运算符
- 地址运算符
#include <iostream>
using namespace std;
#define pri() cout<<"line: "<<__LINE__<<endl;
class Student{
public:
Student(){ pri(); }
void setValue(int age, float weight){
this->age = age;
this->weight = weight;
}
int getAge(){
return age;
}
float getWeight(){
return weight;
}
private:
int age;
float weight;
};
int main(int argc, char *argv[])
{
Student stu;
#if 1
stu.setValue(18, 120);
Student stu1;
stu1 = stu;
cout << stu1.getAge() << endl;
Student *p;
p = &stu;
cout << p->getWeight() << endl;
Student stu2 = stu1;
cout << stu2.getAge() << endl;
Student stu3(stu1);
cout << stu3.getAge() << endl;
Student *q = new Student;
#endif
}
5.3、构造和析构
1、构造
构造函数的目的: 用来初始化对象
特点:
1、默认构造函数没有显式说明时,会自动生成
2、构造函数没有数据类型,也就是没有返回值
3、构造函数函数函数名 与 类名一致
4、构造函数只能在定义对象时,被系统自动调用
5、构造函数可以重载
6、构造函数可以有默认参数
7、构造函数是类的 特殊成员函数
8、构造函数一般声明在 public 区域,也可以声明在 protected和private 区域形成限制构造
#include <iostream>
using namespace std;
#define pri() cout<<"line: "<<__LINE__<<endl;
class Student{
public:
Student(){ pri(); }
Student(int a){ pri(); }
Student(Student &obj){ pri(); }
private:
int age;
float weight;
};
int main(int argc, char *argv[])
{
Student stu;
Student stu1(1);
Student stu3(stu1);
Student stu2 = stu1;
#if 1
Student *q = new Student;
q = new Student(2);
q = new Student(stu);
#endif
}
2、拷贝构造
1、浅拷贝:同类型的两个对象的成员指针,指向同一个堆区空间,两成员指针相互干扰
2、深拷贝:同类型的两个对象的成员指针,指向两个不同的堆区,互不干扰
#include <iostream>
#include <string.h>
using namespace std;
class Student{
public:
Student(const char *name, int age);
Student(Student &obj);
char *getName();
void setName(const char *name);
private:
char *name;
int age;
};
Student::Student(const char *name, int age)
{
this->name = new char[ strlen(name) + 12 ];
strcpy(this->name, name);
this->age = age;
}
Student::Student(Student &obj)
{
#if 0
this->name = obj.name;
#else
this->name = new char[strlen(obj.name) + 1];
strcpy(this->name, obj.name);
#endif
this->age = obj.age;
}
char * Student::getName()
{
return name;
}
void Student::setName(const char *name)
{
strcpy(this->name, name);
}
int main(int argc, char *argv[])
{
Student stu("张三", 18);
Student stu1(stu);
cout << "stu1: " << stu1.getName() << endl;
stu1.setName("张三疯");
cout << "stu1: " << stu1.getName() << endl;
cout << "stu: " << stu.getName() << endl;
}
3、补充-考题
#include <iostream>
using namespace std;
#define pri() cout<<"line: "<<__func__<<endl;
class A{
public:
A(){ pri(); }
A(A &obj){}
};
class B{
public:
B() { pri(); }
};
class Demo{
public:
B c;
Demo(){ pri(); }
private:
A a;
A b;
};
int main(int argc, char *argv[])
{
Demo obj;
}
4、析构函数
1、析构函数:作用用来回收对象占用的资源
特点:
1、没有数据类型,也就是没有返回值
2、函数名由 ~类名 构成
3、析构没有参数和返回值,因此不能被重载。
4、对象生命周期结束时,系统自动调用
5、对象指针遇见 delete 时,系统自动调用
2、包含对象成员(另一个类的对象最为改类的成员变量)的类对象的构造顺序;
构造顺序:
先对象成员构造,再对象构造
析构顺序:
先对象析构,再对象成员析构
#include <iostream>
#include <string>
using namespace std;
#define pri() cout << "line" << __LINE__ << endl;
class Base{
public:
Base()
{
p = new char[12];
pri();
}
~Base()
{
delete []p;
pri();
}
private:
char *p;
};
int main(int argc, char *argv[])
{
Base obj;
Base *p = new Base;
delete p;
}
5.4、this指针
this指针是一个特殊指针,指向类对象自身的首地址。
练习: 设计一个 Point 类描述坐标点,定义 p1点(0,4),p2点(3,0),计算两点之间直线距离
cout << p1.dis(p2) << endl; ==> 5
#include <iostream>
#include <cmath>
using namespace std;
class Point{
public:
Point(){}
Point(int a, int b)
{
x = a;
y = b;
}
double Distance(Point a,Point b);
private:
int x, y;
};
double Point::Distance(Point a,Point b)
{
return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}
int main()
{
Point point, p1(0,4), p2(3,0);
cout << point.Distance(p1, p2);
}
#include <iostream>
using namespace std;
#include <math.h>
class Point{
public:
Point(int x, int y)
{
this->x = x;
this->y = y;
}
float dis(Point &obj)
{
return sqrt( (x-obj.x)*(x-obj.x) + (y-obj.y)*(y-obj.y) );
}
private:
int x, y;
};
int main(int argc, char *argv[])
{
Point p1(3, 0), p2(0, 4);
cout << p1.dis(p2) << endl;
}
5.5、static 修饰成员
1、静态成员变量
1、在整个类域都可见,与具体的对象无关,
static 改变了生命周期,可以作用类型的不同对象之间的数据传递。
2、必须在类体之外定义初始化。
2、静态成员函数:
1、声明时必须有 staic 关键字,定义时不能有 static。
2、函数体中不能访问类的非静态成员。因为这里面没有this指针,也是语法规定的。
3、静态修饰对象:就是修饰一个变量,改变生命周期。
#include <iostream>
using namespace std;
class Base{
public:
static int z;
Base(){}
Base(int x, int y, int z)
{
this->x = x;
this->y = y;
this->z = z;
}
int getX()
{
return x;
}
static void setValue(int , int , int);
protected:
int y;
private:
static int x;
};
int Base:: x = 13;
int Base:: z = 0;
void Base:: setValue(int x, int n, int z)
{
Base:: x = x;
y = n;
Base:: z = z;
}
int main(int argc, char *argv[])
{
Base obj(1, 2, 3);
Base obj1;
cout << obj1.getX() << endl;
cout << Base::z << endl;
cout << obj1.z << endl;
Base::setValue(1, 2, 3);
obj.setValue(2, 3, 4);
}
5.6、const 修饰成员
1、const 修饰成员变量,表示只读,必须在构造函数 参数初始化列表中赋初值
1、参数初始化列表语法: //只能在定义函数时写,声明时不能写
类名(<形参列表>):成员变量(形参1),成员变量(形参2)...
{}
说明:
1、程序在执行时,调用构造函数先执行参数初始化列表,然后才执行 构造函数体
2、包含const 成员变量的类的所有构造函数都必须写出参数初始化列表,为只读变量赋初值。
2、const 修饰成员函数
1、声明定义都必须使用 const 关键字
2、表示不能使用该函数修改任何的成员变量,但是可以访问成员变量
3、const 修饰成员对象:
1、表示该对象定义初始化之后,任何属性都不能更改
2、const 修饰的对象不能访问类的非const 成员函数
#include <iostream>
using namespace std;
class Base{
public:
Base();
Base(int x, int y) : x(x), y(y)
{
}
int getX()
{
return x;
}
int setValue(int x, int y) const;
int z;
private:
const int x;
int y;
};
Base::Base():x(0)
{
}
int Base::setValue(int x, int y) const
{
int a = this->y;
}
int main(int argc, char *argv[])
{
#if 0
Base obj(1, 3);
cout << obj.getX() << endl;
#endif
const Base obj;
Base const obj1(1, 3);
obj.setValue(0, 3);
int x = obj.z;
}
5.7、友元
1、友元函数
1、一个友元函数可以是多个类的有元函数,只需要在各个类中分别声明。
2、友元函数: 就是在成员函数声明之前使用 friend 关键字说明,此时该函数就打破类的封装:
1、友元函数不再是类的成员函数,不能通过对象 . 号访问,只能像普通函数调用一样使用
2、友元函数的函数体中,没有类体的访问权限限定,也就是可以直接通过 对象.号 访问非静态成员,可以通过 类名::静态成员
3、友元函数如果没有类类型作为形参,那么必须在类的外部进行定义,否则声明定义都可以在类体中
4、友元函数必须声明在类体中
#include <iostream>
using namespace std;
class Base{
public:
Base(int x):x(x){}
friend int getValue(Base &obj)
{
return obj.x;
}
friend int getX();
private:
int x;
static int y;
};
int Base:: y = 13;
int getX()
{
Base obj(3);
obj.x = 5;
return Base::y;
}
int main(int argc, char *argv[])
{
Base obj(12);
cout << getValue(obj) << endl;
cout << getX() << endl;
}
2、友元类
1、友元关系不能被继承
2、友元关系是单向的,不具有交互性。
3、如果 B 是 A 的友元类,也就是 A 将 B 当成朋友:
那么在 B 类的所有成员函数中将 打破 A 类的权限。
#include <iostream>
using namespace std;
class A{
public:
A(int x, int y):x(x){}
friend class B;
private:
int x;
static int y;
};
int A::y = 13;
class B{
public:
int getY()
{
A obj(1, 2);
obj.x = 3;
return A::y;
}
int getX(A &obj)
{
return obj.x;
}
};
int main(int argc, char *argv[])
{
A a(3, 4);
B b;
cout << b.getY() << endl;
cout << b.getX(a) << endl;
}
3、有元成员函数
1、B类的成元函数可以是类A的友元
#include <iostream>
using namespace std;
class A;
class B{
public:
int getX(A &obj);
int getY(A &obj);
};
class A{
public:
A(int x):x(x){}
friend int B::getX(A &);
private:
int x;
static int y;
};
int A::y = 15;
int B::getX(A &obj)
{
obj.y = 11;
return obj.x;
}
int B::getY(A &obj)
{
}
int main(int argc, char *argv[])
{
}
练习:
// 思考:
// 1、友元类和友元成员函数中,构造、析构的执行顺序
// 2、设计一个Point类,定义 p1(3, 0), p2(0, 4),
// cout << dis(p1, p2) << endl; 求两点之间的直线距离
// 3、分别定义一个类A和类B ,各有一个私有整数成员变量通过构造函数初始化;
// 类A有一个成员函数Show(B &b)用来打印A和B的私有成员变量,
// 请分别通过友元成员函数和友元类来实现此功能。
1、友元类和友元成员函数中,构造、析构的执行顺序先被调用的构造函数,
其对应(同一对象中的)析构函数最后被调用,而最后被调用的构造函数,其对应的析构函数最先被调用。
#include <iostream>
#include <cmath>
using namespace std;
class Point{
public:
Point(){}
Point(int x, int y)
{
this->x = x;
this->y = y;
}
friend double dis(Point x, Point y);
private:
int x, y;
};
double dis(Point x, Point y)
{
return sqrt( pow(x.x-y.x, 2) + pow(x.y-y.y, 2) );
}
int main()
{
Point p1(3, 0), p2(0, 4);
cout << dis(p1, p2) << endl;
}
#include <iostream>
using namespace std;
class B;
class A{
public:
A(int x):x(x){}
void Show(B &);
private:
int x;
};
class B{
public:
B(int x):x(x){}
private:
friend void A::Show(B &);
friend class A;
int x;
};
void A::Show(B &b)
{
cout << "A: " << x << endl;
cout << "B: " << b.x << endl;
}
int main(int argc, char *argv[])
{
A a(2);
B b(4);
a.Show(b);
}
六、运算符重载
运算符重载:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
不能被重载的运算符
逗号"." ?号表达式"?:" sizeof 作用域"::"
6.1、成员运算符重载函数
函数体中有this指针指向第一个操作数,重载函数的形参个数 = 操作数 - 1;除了b++或b--
#include <iostream>
using namespace std;
class Base{
public:
Base(int x) : x(x){}
int operator+(int a)
{
return x + a;
}
int operator-(int a);
int operator++(int )
{
return x++;
}
int operator++()
{
return ++x;
}
private:
int x;
};
int Base::operator-(int a)
{
return x - a;
}
int main(int argc, char *argv[])
{
Base b(3);
int a = b + 2;
cout << a << endl;
a = b.operator-(2);
cout << a << endl;
cout << b++ << endl;
cout << ++b << endl;
}
6.2、友元运算符重载函数
函数体中没有this指针指向第一个操作数,重载函数的形参个数 = 操作数;除了b++或b--
重载函数的第一个形参,表示第一个操作数
#include <iostream>
using namespace std;
class Base{
public:
Base(int x) : x(x){}
friend int operator+(int a, Base &b)
{
return a + b.x;
}
friend int operator++(Base &obj, int);
friend int operator-(Base &b, int a)
{
return b.x - a;
}
friend ostream & operator<<(ostream &out, Base &obj)
{
out << obj.x;
return out;
}
friend istream & operator>>(istream &in, Base &obj)
{
in >> obj.x;
return in;
}
int operator()();
private:
int x;
};
int operator++(Base &obj, int)
{
return obj.x++;
}
int Base::operator()()
{
return x;
}
int main(int argc, char *argv[])
{
Base b(3);
cout << 2 + b << endl;
cout << b++ << endl;
cout << b - 2 << endl;
cout << b << endl;
Base a(1);
cin >> a >> b;
cout << "a: " << a << "b: " << b << endl;
cout << a() << endl;
}
#include <iostream>
using namespace std;
class Base{
public:
Base(int x) : x(x){}
int operator()();
int operator()(int a);
private:
int x;
};
int Base::operator()()
{
return x;
}
int Base::operator()(int a)
{
return x + a;
}
int main(int argc, char *argv[])
{
Base obj(10);
cout << obj() << endl;
cout << obj(5) << endl;
}
6.3、函数数据类型
1、一般情况下,由返回值决定。
2、返回类的成员变量,并且期望返回之后能改变成员变量的数值,则返回引用类型。
练习:
1、设计 Mytring 类,模仿string 类
2、设计链表类, +号表示,链表尾部插入数据, -号表示尾部删除数据
3、重载 -> 、* 、new[] 三个运算符
七、模板
八、类继承
九、多态
十、异常
十一、转换函数
十二、智能指针
跳转:上一篇、网络编程学习总结!
跳转:上一篇、网络编程学习总结!
跳转:C++ 理论概念知识总结!
跳转:C++ 理论概念知识总结!
持 续 更 新 中 . . .
|