// class.cpp : Defines the entry point for the console application.
//
include "stdafx.h"
include <iostream>
include <string>
using namespace std;
void inr(int& m);
void vor(int m);
class Animal //-------------------父類
{
private: //私有
char name[20];
protected://保護(hù)類型
int age;
public :
void show(char* na ,int a);
};
class Cat : public Animal //子類--------------------
{
public:
void fly();
};
void Cat::fly()
{
cout << "Im's cat,i don't fly!" << endl;
}
void Animal ::show(char* na ,int a)
{
strcpy(name,na);
this->age = a;
cout <<"hello,my name is"<< name<<"\n"<<"and my sge is"<<age<<endl;
}
int main(int argc, char* argv[])
{
Animal A;
A.show("tiger",15);
Cat B;
B.show("laoshu",18); //因?yàn)槭枪欣^承animal的私有元素锚赤。。蔗喂。所以能像Animal類一樣使用公開的和保護(hù)的函數(shù)
B.fly();
int m = 15; //c
vor(m); //形參的傳入鸣峭,相當(dāng)于是m的復(fù)制一份給vor函數(shù)筒狠,用完消失
cout << m <<endl;
inr(m); //數(shù)值地址傳入,是對(duì)地址進(jìn)行修改。(引用和取別名)是對(duì)源參數(shù)的再次命名冤今。摩桶。而指針是對(duì)指針指向進(jìn)行交換桥状。
cout << m <<endl;
return 0;
}
void inr(int& m)
{
++m;
}
void vor(int m)
{
++m;
}
void swap (int* a, int* b)
{
int * t ;
*t = *a;
a =b;
b =t
}
void swap(&a,&b)
{
int t ;
t = a;
a = b;
b = t
}
// 公 -- 保護(hù) -- 私有
// 父類 √ --√ --√
// 子類 √ --√ --×
// 外 √ --× --×