第二次作业。
This commit is contained in:
40
OOP/02/Exercise01.cpp
Normal file
40
OOP/02/Exercise01.cpp
Normal file
@@ -0,0 +1,40 @@
|
||||
#include <iostream>
|
||||
|
||||
class Student {
|
||||
private:
|
||||
int ID, grade;
|
||||
|
||||
public:
|
||||
Student() {
|
||||
this->ID = 0;
|
||||
this->grade = 0;
|
||||
};
|
||||
|
||||
Student(int newID, int newGrade) : ID(newID), grade(newGrade){};
|
||||
|
||||
void display();
|
||||
|
||||
void set(int, int);
|
||||
};
|
||||
|
||||
void Student::display() {
|
||||
std::cout << "ID: " << this->ID << ", Grade: " << this->grade << std::endl;
|
||||
}
|
||||
|
||||
void Student::set(int newID, int newGrade) {
|
||||
this->ID = newID;
|
||||
this->grade = newGrade;
|
||||
}
|
||||
|
||||
int main() {
|
||||
Student stu[5];
|
||||
stu[0].set(21394,89);
|
||||
stu[1].set(2394888, 100);
|
||||
stu[2].set(299, 70);
|
||||
stu[3].set(3999, 10);
|
||||
stu[4].set(4999, 60);
|
||||
stu[0].display();
|
||||
stu[2].display();
|
||||
stu[4].display();
|
||||
return 0;
|
||||
}
|
||||
47
OOP/02/Optional02.cpp
Normal file
47
OOP/02/Optional02.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <iostream>
|
||||
#include <stdlib.h>
|
||||
|
||||
class Strings {
|
||||
public:
|
||||
Strings(char *s);
|
||||
~Strings();
|
||||
void Print();
|
||||
void Set(char *s);
|
||||
|
||||
private:
|
||||
int length;
|
||||
char *str;
|
||||
};
|
||||
|
||||
Strings::Strings(char *s) {
|
||||
length = strlen(s);
|
||||
str = (char *)malloc(length + 1);
|
||||
strcpy(str, s);
|
||||
}
|
||||
|
||||
Strings::~Strings() {
|
||||
free(str);
|
||||
}
|
||||
|
||||
void Strings::Print() {
|
||||
std::cout << str << std::endl;
|
||||
}
|
||||
|
||||
void Strings::Set(char *s) {
|
||||
free(str);
|
||||
length = strlen(s);
|
||||
str = (char *)malloc(length + 1);
|
||||
strcpy(str, s);
|
||||
}
|
||||
|
||||
int main() {
|
||||
Strings* str1 = new Strings("didig");
|
||||
Strings* str2 = new Strings("dirir");
|
||||
|
||||
str1->Print();
|
||||
str2->Print();
|
||||
str1->Set("ifirk");
|
||||
str1->Print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user