十九. 函数重载
#include <iostream>
using namespace std;
//函数的返回值,函数形参列表(参数的个数,参数类型,参数顺序)
//函数重载:函数名相同,参数列表不同
int func(int a, int b)
{
cout<<"func1"<<endl;
return 0;
}
int func(int a)
{
cout<<"func2"<<endl;
return 0;
}
int g(int a, int b)
{
cout<<"g1"<<endl;
return 0;
}
//如果我们要是使用函数重载的话,那么不要写默认参数,避免调用时候出现函数冲突
int g(int a, int b, int c = 300)
{
cout<<"g2"<<endl;
return 0;
}
int main()
{
func(10);
func(10,20);
g(10,20,30);
//g(10,20); //这里的这种方法,我们不能确定是调用g1还是g2,因此会出现错误
//就函数重载的调用规则:
//1. 如果能够严格匹配,那么调用严格匹配的
//2. 如果没有严格匹配的,则调用隐式转换
//3. 如果都匹配不到,则调用失败
return 0;
}
二十. 函数重载和函数指针
#include <iostream>
using namespace std;
int func(int a, int b)
{
cout<<"func(int a, int b)"<<endl;
return 0;
}
int func(int a,int b,int c)
{
cout<<"func(int a,int b,int c)"<<endl;
return 0;
}
//1. 定义一种函数类型
typedef int(MY_FUNC)(int, int);
//2. 定义一个指向函数类型的指针类型
typedef int(*MY_FUNC_P)(int,int);
int main()
{
//Method 1
MY_FUNC *fp = NULL;
fp = func;
fp(10,20);
//Method 2
MY_FUNC_P fq = NULL;
fq = func;
fq(20,10);
//Method 3
int(*fp3)(int, int) = NULL;
fp3 = func;
fp3(10,20);
cout<<"-----------------"<<endl;
fp3 = func;//fp3 ----->func(int,int)
fp3(10,20);//ok,在这个时候fp3,在之前就已经被固定,是不能重载的
//fp3(10,20,30);//Not Ok
//实际上再给函数指针赋值的时候,是会发生函数重载匹配的
//在调用函数指针的时候,所调用的函数就已经固定了
int(*fp4)(int,int,int) = NULL;
fp4 = func;
fp4(10,30,20);
return 0;
}
二十一. 类的基本概念
#include <iostream>
#include <string.h>
using namespace std;
struct Hero
{
char name[64];
int sex;
};
void printHero(struct Hero &h)
{
cout<<"name = "<< h.name <<endl;
cout<<"sex = "<< h.sex <<endl;
}
class AdvHero
{
public://访问控制权限
char name[64];
int sex;
void printHero()
{
cout<<"AdvHero::printHero()"<<endl;
cout<<"name = "<< this->name <<endl;
cout<<"sex = "<< this->sex <<endl;
}
};
class Animal
{
//{}以内 叫类的内部,以外叫类的外部
public:
char kind[64];
char color[64];
//在public下面定义的成员变量和函数,在类的内部和外部都是可以访问的
void printAnimal()
{
cout<<"kind = "<<kind<<endl;
cout<<"color = "<<color<<endl;
}
void write()
{
cout<<kind<<" 开始写字了 "<<endl;
}
void run()
{
cout<<kind<<" 开始跑起来了 "<<endl;
}
private:
//在private下面定义的成员变量和函数,只能在类的内部访问
};
int main()
{
Hero h;
strcpy(h.name,"gailun");
h.sex = 1;
printHero(h);
cout<<"--------------------"<<endl;
AdvHero advH;
strcpy(advH.name, "ChunBro");
advH.sex = 1;
advH.printHero();
cout<<"--------------------"<<endl;
Animal dog;
strcpy(dog.kind,"dog");
strcpy(dog.color,"yellow");
Animal sheep;
strcpy(sheep.kind,"sheep");
strcpy(sheep.color,"white");
dog.write();
sheep.run();
return 0;
}
二十二. 类的封装
#include <iostream>
using namespace std;
//一个类的内部,默认的访问控制权限是private
//一个结构体的内部,默认访问控制权限是public
struct Date
{
int year;
int month;
int day;
};
void init_date(struct Date & d)
{
cout << " year, month, day"<<endl;
cin >> d.year;
cin >> d.month;
cin >> d.day;
}
//打印date的接口
void print_date(struct Date & d)
{
cout<<d.year<<" 年 "<<d.month<<" 月 "<<d.day<<" 日 "<<endl;
}
bool id_leap_year(struct Date &d)
{
if((d.year % 4 ==0) && (d.year%100 !=0)||(d.year%400 ==0))
return true;
else
return false;
}
class MyDate
{
public:
MyDate(int year, int month,int day)
{
this->year = year;
this->month = month;
this->day = day;
}
void print_date()
{
cout<<this->year<<" 年 "<<this->month<<" 月 "<<this->day<<" 日 "<<endl;
}
void id_leap_year()
{
if((this->year % 4 ==0) && (this->year%100 !=0)||(this->year%400 ==0))
cout<<"这是闰年"<<endl;
else
cout<<"这不是闰年"<<endl;
}
int get_year()
{
return this->year;
}
void set_year(int year)
{
this->year = year;
}
private:
int year;
int month;
int day;
};
int main()
{
#if 0
Date d1;
init_date(d1);
print_date(d1);
if(id_leap_year(d1))
cout<<"这是闰年"<<endl;
else
cout<<"这不是闰年"<<endl;
cout<<"-----------------------------"<<endl;
#endif
int year,month,day;
cout << " year, month, day"<<endl;
cin >> year;
cin >> month;
cin >> day;
MyDate mydate(year,month,day);
mydate.print_date();
mydate.id_leap_year();
cout<<"-----------------------------"<<endl;
cout<<"The year is:"<< mydate.get_year()<<endl;
mydate.set_year(2012);
mydate.print_date();
mydate.id_leap_year();
return 0;
}