第三周:程序设计预算法(三)

题面

001:返回什么才好呢

未懂

1.A& GetObj()//没有明白此处的this指针,定义这个函数的作用为没有明白

#include <iostream>
using namespace std;
class A {
public:
	int val;

	A(int
// 在此处补充你的代码
 n=123)
	{
		val = n;
	}
	A& GetObj()//没有明白此处的this指针,定义这个函数的作用为没有明白
	{
		return *this;
	}
};
int main()
{
	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;
}

002:Big & Base 封闭类问题

#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){}//忘记叫啥链表了,反正是赋值用的!
};
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;
	}
}

003:编程填空:统计动物数量(不会标记

恕我直言整道题我都不懂

#include <iostream>
using namespace std;
// 在此处补充你的代码
class Animal{
public:
    static int number;
    Animal(){
        number++;
    }
    virtual ~Animal()
    {
        number--;
    }
};
int Animal::number=0;
class Dog:public Animal{
public:
    static int number;
    Dog()
    {
        number++;
    }
    ~Dog()
    {
        number--;
    }
};
int Dog::number=0;
class Cat :public Animal{
public:
    static int number;
    Cat()
    {
        number++;
    }
    ~Cat()
    {
        number--;
    }
};
int Cat::number=0;
void print() {
	cout << Animal::number << " animals in the zoo, " << Dog::number << " of them are dogs, " << Cat::number << " of them are cats" << endl;
}
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();
}

004:这个指针哪来的

要学常变量对象的定义形式

#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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值