编程打卡:面向对象程序设计测试
面向对象程序设计测试
#include <iostream>
#include<iomanip>
using namespace std;
class Point
{
protected:
double x,y;
public:
Point(double a = 0,double b = 0):x(a),y(b) {
cout << "Point constructor called\n";
};
~Point () {
cout << "Point destructor called\n";
};
};
class Circle
{
protected:
Point p1;
double r;
public:
Circle(double a = 0,double b = 0,double c = 0):p1(a,b),r(c) {
cout << "Circle constructor called\n";
};
~Circle () {
cout << "Circle destructor called\n";
};
double getCircumference() {
return 2*3.14*r;
}
};
int main()
{
float x,y,r;
cin>>x>>y>>r;
Circle c(x,y,r);
cout<<fixed<<setprecision(2)<<c.getCircumference()<<endl;
return 0;
}
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Person
{
protected:
string name;
int age;
public:
Person() : name("person"), age(0){};
Person(string p_name, int p_age) : name(p_name), age(p_age){};
void display() { cout << name << ":" << age << endl; }
};
class Student : public Person
{
protected:
int ID;
float cpp_score;
float cpp_count;
float cpp_grade;
public:
Student(string n,int a,int sid,float score,float count):Person(n,a),ID(sid),cpp_score(score),cpp_count(count)
{
cpp_grade = cpp_score * 0.9 + cpp_count * 2;
};
void print()
{
cout << ID << " " << name << " " << setiosflags(ios::fixed) << setprecision(1) << cpp_grade << endl;
}
};
int main()
{
while (1)
{
string name;
int sid,age;
float score,count;
cin >> name;
if (name == "0")
exit(0);
cin >> sid >> age >> score >> count;
Student s1(name,age,sid,score,count);
s1.print();
}
}
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Person
{
protected:
string name;
int age;
public:
Person() : name("person"), age(0){};
Person(string p_name, int p_age) : name(p_name), age(p_age){};
void display() { cout << name << ":" << age << endl; }
};
class Student : public Person
{
protected:
int ID;
float cpp_score;
float cpp_count;
float cpp_grade;
public:
Student(){};
Student(string n,int a,int sid,float score,float count):Person(n,a),ID(sid),cpp_score(score),cpp_count(count)
{
cpp_grade = cpp_score * 0.9 + cpp_count * 2;
};
void print()
{
cout << ID << " " << name << " " << setiosflags(ios::fixed) << setprecision(1) << cpp_grade << endl;
}
float getGrade()
{
return cpp_grade;
}
};
class Teacher : public Person
{
protected:
int ID;
Student stu[100];
int count;
float cpp_average;
public:
Teacher(string n,int a,int tid):Person(n,a),ID(tid),count(0),cpp_average(0){};
void Add (Student & stu1) {
stu[count] = stu1;
count++;
}
void average() {
float sum = 0;
for (int i = 0; i < count; i++)
{
sum += stu[i].getGrade();
}
cpp_average = sum / count;
}
void print() {
cout << ID << " " << name << " " << count << " " << setiosflags(ios::fixed) << setprecision(1) << cpp_average << endl;
for (int i = 0; i < count; i++)
{
stu[i].print();
}
}
};
int main()
{
string name;
int age;
int ID;
float cpp_score;
float cpp_count;
cin >> name >> ID >> age;
Teacher t(name, age, ID);
while (1)
{
cin >> name;
if(name == "0") {
break;
}
cin >> ID >> age >> cpp_score >> cpp_count;
Student s(name, age, ID, cpp_score, cpp_count);
t.Add(s);
}
t.average();
t.print();
}
神奇,这些题用float就能过,用double就过不了。