#include <iostream>
#include <string.h>
using namespace std;
struct Hero
{
char name[64];
int id;
};
void printHero(Hero &h)
{
cout << "name=" << h.name << endl;
cout << "id=" << h.id << endl;
}
class SuperHero
{
public://访问控制权限
char name[64];
int id;
//类内部函数(方法)调用类内部变量时,不需要传参了,可以直接调用,因为都是在同一个类的内部,函数内调用的变量会直接在类内部匹配,且不会发生歧义,类内部函数又被叫做方法
void printHero()
{
cout << "name=" << name << endl;
cout << "id=" << id << endl;
}
};
class Dog
{
//public权限时,在类的内部和类的外部都可以直接访问类里面的变量和方法(函数)
public:
char name[64];
char color[64];
//函数参数里的int age为外部调用函数传进来的参数
void printDog(int age)
{
//this->age表示是这个类内部定义的变量age,this->age和int age是完全两个不同的变量
this->age = age;
cout << "age=" << this->age << endl;
cout << "name=" << name << endl;
cout << "color=" << color << endl;
}
//private权限时,只有类的内部可以访问此权限下面的变量和方法(即函数),类的外部不可以访问
private:
int age;
//protected权限时,在单个类中,和private关键字的权限是一样的,只有在类的继承中,才和private有区别
protected:
};
class Date
{
public:
void initDate(int year, int mouth, int day)
{
this->year = year;
this->mouth = mouth;
this->day = day;
}
/*
//这种方法初始化数据时,year,mouth,day变量是直接与类内部私有变量进行匹配,不会与别的位置进行匹配???
void initDate()
{
cin >> year >> endl;
cin >> mouth >> endl;
cin >> day >> endl;
}
*/
void printDate()
{
cout << "year=" << year << endl;
cout << "mouth=" << mouth << endl;
cout << "day=" << day << endl;
}
bool isLeapYear()
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
cout << year << " is leap year" << endl;
return true;
}
cout << year << " is not leap year" << endl;
return false;
}
//为了获取和改变类的私有变量,c++同意了命名方法,get...()和set...()
int getYear()
{
return year;
}
int setYear(int year)
{
this->year = year;
}
private:
int year;
int mouth;
int day;
};
//一个类的成员变量的默认访问权限为private,类的本质是一个类型,对象的本质是变量
class Date1
{
int year;
};
//一个结构体的成员变量的默认访问权限为public
struct Date2
{
int year;
};
int main()
{
Hero h = { "xuqiang", 20 };
printHero(h);
//SuperHero是一种类型,被叫做类,hero是一个SuperHero类的变量,被叫做对象
SuperHero hero;
hero.id = 100;
strcpy_s(hero.name, sizeof("xuxiaoyang") + 1, "xuxiaoyang");
hero.printHero();
Dog dog;
//因为Dog类里的成员变量color和name都是public访问权限,所以在类外部可以直接对这两个类内部成员变量进行赋值
strcpy_s(dog.color, strlen("yellow") + 1, "yellow");
strcpy_s(dog.name, strlen("dahuang") + 1, "dahuang");
//在类的外部不可以直接访问类内部private权限下的变量和方法(函数),但可以通过调用public权限下的方法对私有变量进行访问修改
dog.printDog(3);
Date date;
int year, mouth, day;
cin >> year >> mouth >> day;
/*
//等价于下面三段代码
cin >> year;
cin >> mouth;
cin >> day;
*/
date.initDate(year, mouth, day);
date.printDate();
date.isLeapYear();
return 0;
}
#include <string.h>
using namespace std;
struct Hero
{
char name[64];
int id;
};
void printHero(Hero &h)
{
cout << "name=" << h.name << endl;
cout << "id=" << h.id << endl;
}
class SuperHero
{
public://访问控制权限
char name[64];
int id;
//类内部函数(方法)调用类内部变量时,不需要传参了,可以直接调用,因为都是在同一个类的内部,函数内调用的变量会直接在类内部匹配,且不会发生歧义,类内部函数又被叫做方法
void printHero()
{
cout << "name=" << name << endl;
cout << "id=" << id << endl;
}
};
class Dog
{
//public权限时,在类的内部和类的外部都可以直接访问类里面的变量和方法(函数)
public:
char name[64];
char color[64];
//函数参数里的int age为外部调用函数传进来的参数
void printDog(int age)
{
//this->age表示是这个类内部定义的变量age,this->age和int age是完全两个不同的变量
this->age = age;
cout << "age=" << this->age << endl;
cout << "name=" << name << endl;
cout << "color=" << color << endl;
}
//private权限时,只有类的内部可以访问此权限下面的变量和方法(即函数),类的外部不可以访问
private:
int age;
//protected权限时,在单个类中,和private关键字的权限是一样的,只有在类的继承中,才和private有区别
protected:
};
class Date
{
public:
void initDate(int year, int mouth, int day)
{
this->year = year;
this->mouth = mouth;
this->day = day;
}
/*
//这种方法初始化数据时,year,mouth,day变量是直接与类内部私有变量进行匹配,不会与别的位置进行匹配???
void initDate()
{
cin >> year >> endl;
cin >> mouth >> endl;
cin >> day >> endl;
}
*/
void printDate()
{
cout << "year=" << year << endl;
cout << "mouth=" << mouth << endl;
cout << "day=" << day << endl;
}
bool isLeapYear()
{
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
{
cout << year << " is leap year" << endl;
return true;
}
cout << year << " is not leap year" << endl;
return false;
}
//为了获取和改变类的私有变量,c++同意了命名方法,get...()和set...()
int getYear()
{
return year;
}
int setYear(int year)
{
this->year = year;
}
private:
int year;
int mouth;
int day;
};
//一个类的成员变量的默认访问权限为private,类的本质是一个类型,对象的本质是变量
class Date1
{
int year;
};
//一个结构体的成员变量的默认访问权限为public
struct Date2
{
int year;
};
int main()
{
Hero h = { "xuqiang", 20 };
printHero(h);
//SuperHero是一种类型,被叫做类,hero是一个SuperHero类的变量,被叫做对象
SuperHero hero;
hero.id = 100;
strcpy_s(hero.name, sizeof("xuxiaoyang") + 1, "xuxiaoyang");
hero.printHero();
Dog dog;
//因为Dog类里的成员变量color和name都是public访问权限,所以在类外部可以直接对这两个类内部成员变量进行赋值
strcpy_s(dog.color, strlen("yellow") + 1, "yellow");
strcpy_s(dog.name, strlen("dahuang") + 1, "dahuang");
//在类的外部不可以直接访问类内部private权限下的变量和方法(函数),但可以通过调用public权限下的方法对私有变量进行访问修改
dog.printDog(3);
Date date;
int year, mouth, day;
cin >> year >> mouth >> day;
/*
//等价于下面三段代码
cin >> year;
cin >> mouth;
cin >> day;
*/
date.initDate(year, mouth, day);
date.printDate();
date.isLeapYear();
return 0;
}