Giraffe Academy
- switch语句
- while循环
- 猜谜游戏
- for循环
- 指数函数
- 二维数组&嵌套循环
- 注释
- *指针(变量)
- *类&对象
- *构造函数
- *对象函数(实例函数)
- *Getters & Setters 获取与设定?
- 继承
string getDayofWeek (int dayNum){ (一般用在需要较多if判断的语句)
string dayName;
switch(dayNum){
case 0:
dayName = “sunday”;
break;
case 1:
dayName = “Monday”;
break;
…
case 6:
dayName = “Saturday”;
break;
default:
dayName = “Invalid Day Number”;
}
return dayName;
}
int main()
{
cout << getDayofWeek(5);
return 0;
}
int index = 1;
while(index <= 5){
cout << index; << endl;
index++;
}
do{
cout << index <
}while(index <= 5);
int secretNum = 7;
int guess;
int guessCount = 0;
int guessLimit = 3;
while(secretNum != guess){
cout << “Enter guess:”;
cin >> guess;
}
cout << “you win!”;
for循环int index = 1;
while(index <= 5){
cout << index << endl;
index++;
}
与while循环对比,for循环类似,但是条件不同
for(int index = 1;index <= 5; index++){
cout << index << endl;
index++;
}
int power(int baseNum,int powNum){
int result = 1;
for(int i = 0;i < powNim;i++){
result *= baseNum;
}
return result;
}
int main()
{
int numberGrid[3][2] = {
{1,2},
{3,4},
{5,6}
};
for(int i =0; i < 3; i++){
for(int j = 0; j < 2; j++){
cout << numberGrid [i][j] << endl;
}
}
}
作为对比,python中的二维数组也类似,[][]
number_grid = [ //这是一个四行三列的矩阵
[1,2,3]
[4,5,6]
[7,8,9]
[0]
]
//
*指针(变量)变量存储在内存中,而指针就是指的这种内存
int age = 19;
double gpa = 2.7;
string name =“Mike”;
cout << &age;
cout << &age; //输出age所在的内存地址
指针变量 *pAge
int age = 19;
int pAge = &age; //将age所在地址保存在pAge
double gpa = 2.7;
double *pGpa = &gpa;
string name =“Mike”;
string *pName = &name;
cout << pAge << endl;
cout << &age << endl;
cout << *pAge;
小结:
int pAge = &age;
其中pAge保存了age所在的地址,而pAge则保存了age所在地址的数字,同理可运用于double和string类型。
其中&age为指针类型,仅可保存于指针变量中,指针变量可以理解是类似int,double,float,string这种数据类型的一种,仅能保存内存地址信息,但也必须结合变量int float double string来使用
另外,专业的说法是,pAge是引用了age的地址,而则是反引用(取消引用),即可以理解为pAge = &age = &&age,*pAge = &&age = age = *&age
实质上,指针只是信息的类型,是另一种我们可以使用的数据类型。
类存在的意义,就是可以让我们通过int,float,string这些数据类型去描述生活中其他“类”的东西,例如描述班级,可以包括每个学生的姓名,成绩,身高等等,从而组成一个特殊的类,一个新的数据类型。
通常的,命名类的时候,一般使用大写字母。
class Book {
public:
string title;
string auther;
int pages;
}; // 这个像是一个蓝图,一个模板,一个规范
/对象是该蓝图,模板,规范的一个实际实例,是类的实际应用,是一个具体应用,在这里,一个对象就是一本真正的书,可能包括数百个实际的书名,数百个实际的作者,数百个实际的书本页数,我可以只写一个书类,然后用这一个书(类)去写数百个书(对象)。/
int main(){
Book book1;//这里Book就和int等用法类似 book1.title = "Harry Potter"; book1.auther = "JK Rowling"; book1.pages = 500; Book book2; book2.title = "Lord of the Rings"; book2.auther = "Tolkein"; book2.pages = 700; cout << book1.title; cout << book2.title;//在这里输出无法直接输出book2 return 0;
}
与python对比:
class student:
def __init__(self,name,major,gpa,is_on_probation): __init__是初始化函数
self.name = name
self.major = major
self.gpa = gpa
self.is_on_probation = is_on_probation
form student(学生文件) import student(学术班级)
student1 = student(“Jim”,“Business”,3.1,False)
student1 = student(“Pam”,“Art”,2.5,True)
print(student1.name)
构造函数是一个特殊的函数,每当我们创建这些书籍对象之一时都会被调用
class Book {
public:
string title;
string auther;
int pages;
Book(){
cout << “Creating Object” <
};
int main(){
Book book1;
Book book2;
return 0;
}//构造函数,每当创建一个对象时,就会使用一次构造函数。
此时会输出
Creating Object
Creating Object
构造函数还可以和其他基本函数一样,输入不同的变量
class Book {
public:
string title;
string auther;
int pages;
Book(string name){
cout << name <
};
int main(){
Book book1(“Harry Potter”);
Book book2(“Load of the Rings”);
return 0;
}
当创建的对象数量很多时,会产生很多很多行的代码,此时,可以通过构造函数来初始化我们的对象。
class Book {
public:
string title;
string auther;
int pages;
Book(string aTitle, string aAuther, int aPages){
title = aTitle;
auther = aAuther;
pages = aPages;
}
Book(){
title = “no title”;
auther = “no Auther”;
pages = 0;//可以多设置几个构造函数,来满足不同的需求
}
};
int main(){
Book book1(“Harry Potter”, “JK Rowling”,500);
book1.title = “sadada”;
Book book2(“Load of the Rings”,“Tolkein”,700);
return 0;
}//利用构造函数,可以大大缩短代码长度
*对象函数(实例函数)对象函数是一个可以放在类中,然后该类的对象可以使用该功能
class Student{
public:
string name;
string major;
double gpa;
Student(string aName, string aMajor,double aGpa){
name = aName;
major = aMajor;
gpa = aGpa;
}//到这里和构造函数相同
bool hasHonors(){
if(gpa >= 3.5){
return true;
}
return false;
}
}
int main(){
Student student1("Jim", "Bussiness",2.4);
Student student2("Pam", "Art", 3.6);
cout << student1.hasHonors();
return 0;
}
小结:构造函数存在于类中,是通过类创建的一个函数,可以理解为类函数。对象函数存在于类中,是在类中创建的一个函数,可以理解为类中函数。
Getters和Setters可以允许你控制对不同属性和不同元素的访问
class Movie{
public:
string title;
string director;
string rating;
Movie(string aTitle, string aDirector,string aRating){
title = aTitle;
director = aDirector;
rating = aRating;
}
};
int main(){
Movie avengers("The Avengers", "Joss Whedon", "PG-13");
}
/G, PG, PG-13, R, NR 电影官方等级就这五种,因此我们必须对rating这一项做出限制,以防止有人给出dog等级(或者其他一些奇奇怪怪的等级)/
cout << avengers.rating;
return 0;
}
当我们说公共时,public:这意味着所有这些东西都是公共的,任何其他程序任何其他代码可以访问它,在这里,任何类之外的代码都可以访问标题,所以在主函数打印电影评分的时候,因为评分是公开的
c++还有一类关键词:private,私人的,私有的,作为对比,如果我将电影评分放在Movie- private- 下,这就意味着只有电影类的代码才能访问评级,如下
class Movie{
private:
string rating;
public:
string title;
string director;
}
此时在主函数,就无法输出电影评级,也无法修改电影评级,此时会报错
cout << avengers.rating;//会报错
avengers.rating = “PG”;//会报错
此时在电影类中,构造函数可以继续访问电影评级,因为此时rating和构造函数在同一个类中,所以能够访问rating,但主函数不能访问。
因此可以利用private和public来控制电影评级rating的设置。
class Movie{
private:
string rating;
public:
string title;
string director;
Movie(string aTitle, string aDirector,string aRating){
title = aTitle;
director = aDirector;
setRating(aRating);
}
void setRating(string aRating){
rating = aRating;
}
};
int main(){
Movie avengers("The Avengers", "Joss Whedon", "PG-13");
avengers.setRating("Dog");
cout << avengers.rating;
}
此时就可以通过->主函数->构造函数->公开函数(对象函数)->修改私人数据
此时可以修改私人数据,但是仍然无法输出私人数据
同时,可以在公开函数中设置
void setRating(string aRating){
if(aRating == “G” || aRating ==“PG” || aRating == “PG-13” || aRating == “R” || aRating == “NR”)
rating = aRating;
}else{
rating = “NR”;
}
}
string getRating(){
return rating;
}
int main(){
cout << getRating();
}
}//通过这个公开函数可以获取私人电影评价信息
小结:私人信息无法在主函数调用出,但是在类中,私人信息是公开的。类中包含了私人信息和公开信息,因此公开信息可以调用私人信息。所以为了调用私人信息,可以设置一个公共函数,通过主函数->公共函数->私人信息->输出到公共函数->主函数。并可以在公共函数设置修改函数,并设置修改函数的格式和类型。
继承继承基本上是我们可以定义一个类的地方,然后我们可以定义其他类,这些类可以扩展功能,或者他们可以继承该原始类的所有功能和所有属性,例如:
class Chef {
public:
void makeChicken(){
cout << “The chef makes chicken” << endl;
}
void makeSalad(){
cout << “The chef makes salad” << endl;
}
void makeSpecialDish(){
cout << “The chef makes bbq ribs” << endl;
}
};
class IntalianChef : public Chef{
void makePasta(){
cout << “The chef makes pasta” << endl;
void makeSpecialDish(){
cout << “The chef makes chicken pam” << endl;//可以改写
}
};
int main()
{
Chef chef;
chef.makeChicken();
IntalianChef intalianChef; intalianChef.makeChicken(); return 0;
}
作为对比,python中的继承也与之类似:
文件1 chef.py
class chef:
def make_chicken(self):
print("The chef makes a chicken")
def make_salad(self):
print("The chef makes a salad")
def make_special_dish(self):
print("The chef makes a bbq ribs")
文件2 chinesechef.py
from chef import chef
class chinesechef(chef):
def make_special_dish(self)
print("the chef make orange chicken")
def make_fried_rice(self)
print("the chef make fried rice")
主程序
from chef import chef
from chinesechef import chinesechef
mychef = chef()
mychef.make_special_dish()
mychinesechef = chinesechef()
mychinesechef.make_chicken()



