#include <iostream>
using namespace std;
class A {
public:
int val;
A(int n = 123)
{
val = n;
};
A & GetObj() {
return *this;
}
};
int main()
{
freopen("f:\\freopen.txt", "r", stdin);
int m, n;
A a;
cout << a.val << endl;
while (cin >> m >> n) {
a.GetObj() = m;
cout << a.val << endl;
a.GetObj() = A(n);
cout << a.val << endl;
}
return 0;
}
//封闭类问题
#include <iostream>
#include <string>
using namespace std;
class Base {
public:
int k;
Base(int n) :k(n) { }
};
class Big
{
public:
int v;
Base b;
Big(int n) :v(n), b(n) { }
Big(const Big & bb) :v(bb.v), b(bb.v) { }
};
int main()
{
int n;
while (cin >> n) {
Big a1(n);
Big a2 = a1;
cout << a1.v << "," << a1.b.k << endl;
cout << a2.v << "," << a2.b.k << endl;
}
}
//统计动物数量
#include <iostream>
using namespace std;
//your code starts here
class Animal {
public:
static int number;
Animal();
virtual ~Animal();
Animal(const Animal & a);
};
int Animal::number = 0;
Animal::Animal() { ++number; }
Animal::~Animal() { --number; }
Animal::Animal(const Animal & a) { ++number; }
class Dog :public Animal
{
public:
static int number;
Dog();
~Dog();
Dog(const Dog & a);
};
int Dog::number = 0;
Dog::Dog() { ++number; }
Dog::~Dog() { --number; }
Dog::Dog(const Dog & a) { ++number; }
class Cat :public Animal
{
public:
static int number;
Cat();
~Cat();
Cat(const Cat & a);
}; int Cat::number = 0;
Cat::Cat() { ++number; }
Cat::~Cat() { --number; }
Cat::Cat(const Cat & a) { ++number; }
//your code ends here
void print() {
cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
int main() {
print();
Dog d1, d2;
Cat c1;
print();
Dog* d3 = new Dog();
Animal* c2 = new Cat;
Cat* c3 = new Cat;
print();
delete c3;
delete c2;
delete d3;
print();
}
//this 的应用
#include <iostream>
using namespace std;
struct A
{
int v;
A(int vv) :v(vv) { }
const A * getPointer() const
{
return this;
}
};
int main()
{
const A a(10);
const A * p = a.getPointer();
cout << p->v << endl;
return 0;
}