#include <iostream>
using namespace std;
class B1{
public:
void output();
};
class B2{
public:
void output();
};
void B1::output()
{
cout << "call the class B1" << endl;
}
void B2::output()
{
cout << "call the class B2" << endl;
}
class A : public B1, public B2
{
public:
void show();
};
void A::show()
{
cout << "call the class A" << endl;
}
void main()
{
A a;
//a.output(); //该语句编译时会报错
a.show();
}
#include<iostream>
using namespace std;
class animal
{
public:
void eat()
{
cout << "animal eat" << endl;
}
void sleep()
{
cout << "animal sleep" << endl;
}
void breath()
{
cout << "animal breath" << endl;
}
};
class fish :public animal
{
public:
void breath()
{
cout << "fish bubble" << endl;
}
};
void fn(animal *pAn)
{
pAn->breath();
}
void main()
{
animal *pAn;
fish fh;
pAn = &fh;
fn(pAn);
system("pause");
}
#include<iostream>
using namespace std;
class animal
{
public:
void eat()
{
cout << "animal eat" << endl;
}
void sleep()
{
cout << "animal sleep" << endl;
}
virtual void breath()
{
cout << "animal breath" << endl;
}
};
class fish :public animal
{
public:
void breath()
{
cout << "fish bubble" << endl;
}
};
void fn(animal *pAn)
{
pAn->breath();
}
void main()
{
animal *pAn;
fish fh;
pAn = &fh;
fn(pAn);
system("pause");
}
#include<iostream>
using namespace std;
void change(int& a, int& b);
void main()
{
int x = 5;
int y = 3;
cout << "original x=" << x << endl;
cout << "original y=" << y << endl;
change(x, y);//此处如果用指针传递,则调用change(&x,&y),这样很容易让人迷惑,不知道交换的是x和y的值,还是x和y的地址?此处为引用,可读性就比指针要好
cout << "changed x=" << x << endl;
cout << "changed y=" << y << endl;
system("pause");
}
//在change()函数的实现中,,采用了一个小算法,完成了a和b值的交换,
void change(int& a, int& b)
{
a = a + b;
b = a - b;
a = a - b;
}