#include
#include
#include
#include
#include
#include
#include
using namespace std;
class Person {
protected:
string firstName;
string lastName;
int id;
public:
Person(string firstName, string lastName, int identification) {
this->firstName = firstName;
this->lastName = lastName;
this->id = identification;
}
void printPerson() {
cout << "Name: "<< lastName << ", " << firstName << "\nID: " << id << "\n";
}
};
class Student : public Person {
private:
vector testScores;
public:
/*
* Class Constructor
*
* Parameters:
* firstName - A string denoting the Person's first name.
* lastName - A string denoting the Person's last name.
* id - An integer denoting the Person's ID number.
* scores - An array of integers denoting the Person's test scores.
*/
// Write your constructor here
Student(string firstName, string lastName, int identification,
vector scores)
: Person(firstName, lastName, identification) {
this->testScores = scores;
}
/*
* Function Name: calculate
* Return: A character denoting the grade.
*/
// Write your function here
string calculate() {
vector::iterator ite;
int sum = 0, averageScore = 0;
for (ite = testScores.begin(); ite < testScores.end(); ite++) {
sum += *ite;
}
averageScore = sum / testScores.size();
if (averageScore >= 90 && averageScore <= 100) {
return "O";
}
else if (averageScore >= 80 && averageScore < 90) {
return "E";
}
else if (averageScore >= 70 && averageScore < 80) {
return "A";
}
else if (averageScore >= 55 && averageScore < 70) {
return "P";
}
else if (averageScore >= 40 && averageScore < 55) {
return "D";
}
else {
return "T";
}
}
};
int main() {
string firstName;
string lastName;
int id;
int numScores;
cin >> firstName >> lastName >> id >> numScores;
vector scores;
for (int i = 0; i < numScores; i++) {
int tmpScore;
cin >> tmpScore;
scores.push_back(tmpScore);
}
Student* s = new Student(firstName, lastName, id, scores);
s->printPerson();
cout << "Grade: " << s->calculate() << "\n";
system("pause");
return 0;
}
class Person:
def __init__(self, firstName, lastName, idNumber):
self.firstName = firstName
self.lastName = lastName
self.idNumber = idNumber
def printPerson(self):
print("Name:", self.lastName + ",", self.firstName)
print("ID:", self.idNumber)
class Student(Person):
# Class Constructor
#
# Parameters:
# firstName - A string denoting the Person's first name.
# lastName - A string denoting the Person's last name.
# id - An integer denoting the Person's ID number.
# scores - An array of integers denoting the Person's test scores.
#
# Write your constructor here
def __init__(self, firstName, lastName, idNumber, scores):
super(Student, self).__init__(firstName, lastName, idNumber)
self.scores = scores
# Function Name: calculate
# Return: A character denoting the grade.
#
# Write your function here
def calculate(self):
averageScores = sum(self.scores) / len(self.scores)
if averageScores<=100 and averageScores>=90:
return "O"
elif averageScores<90 and averageScores>=80:
return "E"
elif averageScores<80 and averageScores>=70:
return "A"
elif averageScores<70 and averageScores>=55:
return "P"
elif averageScores<55 and averageScores>=40:
return "D"
else:
return "T"
line = input().split()
firstName = line[0]
lastName = line[1]
idNum = line[2]
numScores = int(input()) # not needed for Python
scores = list(map(int, input().split()))
s = Student(firstName, lastName, idNum, scores)
s.printPerson()
print("Grade:", s.calculate())
类的继承
子类构造函数的书写
在C++的类继承中,
建立对象时,首先调用基类的构造函数,然后在调用下一个派生类的构造函数,依次类推;
析构对象时,其顺序正好与构造相反;
例子:
#include
using namespace std;
class Shape{
public:
void Draw() {cout<<"Base::Draw()"<
运行结果:
几种情况:
构造方法用来初始化类的对象,与父类的其它成员不同,它不能被子类继承(子类可以继承父类所有的成员变量和成员方法,
但不继承父类的构造方法)。因此,在创建子类对象时,为了初始化从父类继承来的数据成员,系统需要调用其父类的构造方法。
如果没有显式的构造函数,编译器会给一个默认的构造函数,并且该默认的构造函数仅在没有显式地声明构造函数情况下创建。
构造原则如下:
1. 如果子类没有定义构造方法,则调用父类的无参数的构造方法。
2. 如果子类定义了构造方法,不论是无参数还是带参数,在创建子类的对象的时候,首先执行父类无参数的构造方法,
然后执行自己的构造方法。
3. 在创建子类对象时候,如果子类的构造函数没有显示调用父类的构造函数,则会调用父类的默认无参构造函数。
4. 在创建子类对象时候,如果子类的构造函数没有显示调用父类的构造函数且父类自己提供了无参构造函数,则会
调用父类自己的无参构造函数。
5. 在创建子类对象时候,如果子类的构造函数没有显示调用父类的构造函数且父类只定义了自己的有参构造函数,
则会出错(如果父类只有有参数的构造方法,则子类必须显示调用此带参构造方法)。
6. 如果子类调用父类带参数的构造方法,需要用初始化父类成员对象的方式,比如:
#include
class animal
{
public:
animal(int height, int weight)
{
cout<<"animal construct"<
vector的使用,iterator迭代器的使用:
vector 是向量类型,它可以容纳许多类型的数据,如若干个整数,所以称其为容器。vector 是C++ STL的一个重要成员,使用它时需要包含头文件:
#include;
#include;
// 初始化,添加元素
vector a;
for(int i=0;i<10;i++)
a.push_back(i);
//通过下标方式读取
for(int i=0;i<=a.size()-1;i++)
cout<::iterator ite;
for(ite=b.begin();ite!=b.end();ite++)
cout<<*ite<<" ";
参考:
http://www.cnblogs.com/Nonono-nw/p/3462183.html
http://www.cnblogs.com/aminxu/p/4686332.html
类的继承
子类构造函数的书写
list的长度:len(list)
如果在子类中需要父类的构造方法就需要显式地调用父类的构造方法,或者不重写父类的构造方法。
子类不重写 __init__,实例化子类时,会自动调用父类定义的 __init__。
class Father(object):
def __init__(self, name):
self.name=name
print ( "name: %s" %( self.name) )
def getName(self):
return 'Father ' + self.name
class Son(Father):
def getName(self):
return 'Son '+self.name
if __name__=='__main__':
son=Son('runoob')
print ( son.getName() )
输出结果为:
name: runoob
Son runoob
如果重写了__init__ 时,实例化子类,就不会调用父类已经定义的 __init__,语法格式如下:
class Father(object):
def __init__(self, name):
self.name=name
print ( "name: %s" %( self.name) )
def getName(self):
return 'Father ' + self.name
class Son(Father):
def __init__(self, name):
print ( "hi" )
self.name = name
def getName(self):
return 'Son '+self.name
if __name__=='__main__':
son=Son('runoob')
print ( son.getName() )
输出结果为:
hi
Son runoob
如果重写了__init__ 时,要继承父类的构造方法,可以使用 super 关键字:
super(子类,self).__init__(参数1,参数2,....)
还有一种经典写法:
父类名称.__init__(self,参数1,参数2,...)
class Father(object):
def __init__(self, name):
self.name=name
print ( "name: %s" %( self.name))
def getName(self):
return 'Father ' + self.name
class Son(Father):
def __init__(self, name):
super(Son, self).__init__(name)
print ("hi")
self.name = name
def getName(self):
return 'Son '+self.name
if __name__=='__main__':
son=Son('runoob')
print ( son.getName() )
输出结果为:
name: runoob
hi
Son runoob
C++:
https://www.cnblogs.com/AndyJee/p/4575385.html
https://www.cnblogs.com/shmilxu/p/4849097.html
python:
http://www.runoob.com/w3cnote/python-extends-init.html