1题
#include <iostream>
using namespace std;
class MyClass {
public:
MyClass(int len)
{
array = new int[len];
arraySize = len;
for(int i = 0; i < arraySize; i++)
array[i] = i+1;
}
~MyClass()
{
// ERROR **********found**********
delete []array;
}
void Print() const
{
for(int i = 0; i < arraySize; i++)
// ERROR **********found**********
cout<< array[i] << ' ';
cout << endl;
}
private:
int *array;
int arraySize;
};
int main()
{
// ERROR **********found**********
MyClass obj(10);
obj.Print();
return 0;
}
D:\untitled2\cmake-build-debug\untitled2.exe
1 2 3 4 5 6 7 8 9 10
2
#include <iostream>
using namespace std;
class vehicle
{
private:
int MaxSpeed;
int Weight;
public:
//**********found**********
vehicle(int maxspeed, int weight):MaxSpeed(maxspeed),Weight(weight)
{
};
~vehicle(){};
int getMaxSpeed() { return MaxSpeed; }
int getWeight() { return Weight; }
};
//**********found**********
class bicycle : virtual public vehicle
{
private:
int Height;
public:
bicycle(int maxspeed, int weight, int height):vehicle(maxspeed, weight),Height(height){}
int getHeight(){ return Height; };
};
//**********found**********
class motorcar : virtual public vehicle
{
private:
int SeatNum;
public:
motorcar(int maxspeed, int weight, int seatnum):vehicle(maxspeed, weight),SeatNum(seatnum){}
int getSeatNum(){ return SeatNum; };
};
//**********found**********
class motorcycle : public motorcar,public bicycle
{
public:
motorcycle(int maxspeed, int weight, int height):
vehicle(maxspeed, weight),bicycle(maxspeed, weight,height),motorcar(maxspeed, weight, 1){}
};
int main()
{
motorcycle a(80,150,100);
cout<<a.getMaxSpeed()<<endl;
cout<<a.getWeight()<<endl;
cout<<a.getHeight()<<endl;
cout<<a.getSeatNum()<<endl;
return 0;
}
80
150
100
1
3
#include<iostream>
using namespace std;
class Xabc {
int *a; int n;
public:
Xabc(int aa[], int nn) { //构造函数
// ERROR **********found**********
n=nn;
// ERROR **********found**********
a=new int[n];
for(int i=0; i<n; i++) a[i]=aa[i];
}
int GetA(int i) {return a[i];}
~Xabc() { delete []a;}
};
int main() {
int a[5]={2,3,4,5,6};
Xabc x(a,5);
int i, s=0;
// ERROR **********found**********
for(int i=0; i<5; i++) s+=x.GetA(i);
cout<<"s="<<s<<endl;
return 0;
}
D:\untitled4\cmake-build-debug\untitled4.exe
s=20
4
#include<iostream>
#include<string.h>
using namespace std;
class Date { // 日期类
int year, month, day; // 年、月、日
public:
Date(int year, int month, int day) :year(year), month(month), day(day) {}
int getYear()const { return year; }
int getMonth()const { return month; }
int getDay()const { return day; }
};
class Person { // 人员类
char name[14]; // 姓名
bool is_male; // 性别,为 true 时表示男性
Date birth_date; // 出生日期
public:
Person(char *name, bool is_male, Date birth_date)
//**********found**********
:is_male(is_male), birth_date(birth_date)
{
strcpy_s(this->name, name);
}
const char *getName()const { return name; }
bool isMale()const { return is_male; }
Date getBirthdate()const { return birth_date; }
//利用strcmp()函数比较姓名,返回一个正数、0 或负数,分别表示大于、等于、小于
int compareName(const Person &p)const {
//**********found**********
return strcmp(name, p.name);
}
void show() {
cout << endl;
cout << name << ' ' << (is_male ? "男" : "女") << ' ' << "出生日期:"
<< birth_date.getYear() << "年" //显示出生年
//**********found**********
<< birth_date.getMonth() //显示出生月
<< birth_date.getDay() << "日"; //显示出生日
}
};
void sortByName(Person ps[], int size) { //将人员数组按姓名排列为升序
for (int i = 0; i<size - 1; i++) { //采用选择排序算法
int m = i;
for (int j = i + 1; j<size; j++)
if (ps[j].compareName(ps[m])<0) m = j;
if (m>i) {
Person p = ps[m];
ps[m] = ps[i];
ps[i] = p;
}
}
}
int main() {
Person staff[] = {
Person("张三", true, Date(1978, 4, 20)),
Person("王五", false, Date(1965,8,3)),
Person("杨六", false, Date(1965,9,5)),
Person("李四", true, Date(1973,5,30))
};
const int size = sizeof(staff) / sizeof(staff[0]);
int i;
cout << endl << "按姓名排序";
cout << endl << "排序前:";
for (i = 0; i<size; i++) staff[i].show();
sortByName(staff, size);
cout << endl << endl << "排序后:";
for (i = 0; i<size; i++) staff[i].show();
cout << endl;
return 0;
}
结果
网友评论