-
C++第二天
今天晚上系统网络考试,考得真是一塌糊涂,铁定需要补考了。第一阶段的项目至今为止还有两处函数调用出问题没有解决,而且是最重要的两处,这两处解决不了这个项目只能勉强算完成三分之一,这两天一直在查代码,改代码,就是没有进展。今天C++讲的内容不算多,但是现在的感觉又像是刚开始学C基础似的,老师让写个小练习都写不出来,极度缺乏代码量。
fun:函数重载
#include
using namespace std;
//g++ fun.cpp -o fun.o
//objdump -t fun.o
//函数重载
// int biggerii(int a, int b){}
int bigger(int a, int b)
{
cout << "int,int" << endl;
return (a > b ? a:b);
}
//float biggerff(float a, float b){}
float bigger(float a, float b)
{
cout << "float,float" << endl;
return (a > b ? a:b);
}
//double biggerdd(double a, double b){}
double bigger(double a, double b)
{
cout << "double,double" << endl;
return (a > b ? a:b);
}
int main(void)
{
cout << bigger(3, 4) << endl;
// cout << biggerii(3, 4) << endl;
cout << bigger(3.6, 3.67) << endl;
//cout << biggerdd(3.6, 3.67) << endl;
cout << bigger(3.6f, 3.67f) << endl;
// cout << biggerff(3.6f, 3.67f) << endl;
return 0;
}
inline:类似于宏定义
#include
using namespace std;
#define fun(x, y) x*y
//假如一段代码逻辑结构比较简单但是被频繁的调用,那么可以将该段代码定义为inline函数
//逻辑结构比较简单:没有多重条件判断,没有循环结构,没有嵌套调用,没有递归调用等
inline int test(int x, int y)
{
return x*y;
}
int main(void)
{
cout << fun(2+3, 4+5) << endl;
//2+3*4+5
cout << test(2+3, 4+5) << endl;
return 0;
}
可以使用默认值也可以重新赋值
#include
#include
#include
using namespace std;
struct Student
{
int iId;
char caName[32];
float fScore;
char caPwd[32];
};
void show(const Student &stu)
{
cout << stu.iId << ' ' << stu.caName << ' ' << stu.fScore << ' ' << stu.caPwd << endl;
}
#if 0
Student *makeStu(const char *pwd = "123", const char *name){}
#endif
//makeStu(, "lisi"); // x
//makeStu("", "lisi"); // x
//makeStu("123", "lisi"); //
//makeStu("lisi"); // x
//一般将带有默认值的形参写在形参列表最后
Student *makeStu(const char *name, const char *pwd = "abcdefg")
{
static int s_id = 1000;
Student *pstu = (Student *)malloc(sizeof(Student));
pstu->iId = s_id++;
strcpy(pstu->caName,name);
pstu->fScore = 0.0f;
strcpy(pstu->caPwd, pwd);
return pstu;
}
int main(void)
{
Student *stu = makeStu("zhangsan", "123456");
show(*stu);
// Student *s2 = makeStu("lisi");
// show(*s2);
return 0;
}
多态:静态多态---函数重载实现
#include
using namespace std;
//g++ swap.cpp -o swap.o
//objdump -t swap.o
//多态:静态多态---函数重载实现
//编译之后就可以确定到底调用哪个函数,将此过程称之为静态绑定
typedef struct Student
{
int iId;
char caName[32];
float fScore;
}STU;
void show(STU &stu)
{
cout << "id:" << stu.iId << " name:" << stu.caName << " score:" << stu.fScore << endl;
}
void myswap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
cout << "myswap(int)" << endl;
}
void myswap(double &a, double &b)
{
double tmp = a;
a = b;
b = tmp;
cout << "myswap(double)" << endl;
}
void myswap(STU &s1, STU &s2)
{
STU tmp = s1;
s1 = s2;
s2 = tmp;
cout << "myswap(STU)" << endl;
}
int main(void)
{
int a = 90;
int b = 178;
cout << "a=" << a << " b=" << b << endl;
myswap(a, b);
cout << "a=" << a << " b=" << b << endl;
cout << "...................\n";
double d1 = 8.9;
double d2 = 6.7;
cout << "d1=" << d1 << " d2=" << d2 << endl;
myswap(d1, d2);
cout << "d1=" << d1 << " d2=" << d2 << endl;
cout << "...................\n";
STU s1 = {1001, "zhangsan", 89};
STU s2 = {1002, "lisi", 99};
show(s1);
show(s2);
myswap(s1, s2);
show(s1);
show(s2);
return 0;
}
template:模板函数
#include
using namespace std;
typedef struct Student
{
int iId;
char caName[32];
float fScore;
}STU;
void show(STU &stu)
{
cout << "id:" << stu.iId << " name:" << stu.caName << " score:" << stu.fScore << endl;
}
//函数模板
template
void myswap(U &a, U &b)
{
U tmp = a;
a = b;
b = tmp;
}
#if 0
void myswap(int &a, int &b)
{
int tmp = a;
a = b;
b = tmp;
}
#endif
#if 0
void myswap(double &a, double &b)
{
double tmp = a;
a = b;
b = tmp;
}
#endif
#if 0
void myswap(STU &a, STU &b)
{
STU tmp = a;
a = b;
b = tmp;
}
#endif
int main(void)
{
int a = 90;
int b = 178;
cout << "a=" << a << " b=" << b << endl;
myswap(a, b);
cout << "a=" << a << " b=" << b << endl;
cout << "...................\n";
double d1 = 8.9;
double d2 = 6.7;
cout << "d1=" << d1 << " d2=" << d2 << endl;
//函数调用时,函数模板根据传入的具体实参,自动生成具体类型的模板函数。
myswap(d1, d2);
cout << "d1=" << d1 << " d2=" << d2 << endl;
cout << "...................\n";
STU s1 = {1001, "zhangsan", 89};
STU s2 = {1002, "lisi", 99};
show(s1);
show(s2);
myswap(s1, s2);
show(s1);
show(s2);
return 0;
}
new:开辟空间
#include
using namespace std;
int main(void)
{
// int *p = (int *)malloc(sizeof(int));
//int*p2=(int*)malloc(5*sizeof(int));
//malloc(2*3*sizeof(int));
//new/delete 是关键字
int *p = new int;
delete p;//释放空间
int *p2 = new int[5];
delete []p2;
int (*p3)[3] = new int[2][3];
delete []p3;
int (*p4)[4][6] = new int[2][4][6];
delete []p4;
//sizeof(p4) = 4;//32位机
return 0;
}
string:拼接、比较
#include
#include //std::string
using namespace std;
int main(void)
{
string str = "Hello";
cout << str << endl;
//char arr1[32] = "Hello";
//char arr2[32] = {'\0'};
//arr2 = arr1; ?
string str2 = str;
cout << str2 << endl;
//string这种类型对‘+’进行了重新实现
//拼接时:第一个或者第二个必须是string对象
string str3 = str + "world";
//string str3 = "world" + "aaa" + str; // X
str3 = str3 + "abc" + "123";
cout << str3 << endl;
if (str == str1)
// if (str >= str1)
// if (str <= str1)
return 0;
}