Cプログラミングクラスで、プログラムをよりオブジェクト指向にするように変更する割り当てが与えられました。これの一部は、toStringメソッドを修正することでした。道順は次のとおりです。
Modify the Student module to make it more object-oriented.
* Each Student object should have a function pointer that points to an
appropriate function for producing a string representation of the object.
* Provide a default toString method to each object when it is created.
The studentToString method should no longer be part of the Student interface
(as defined in Student.h)
しかし、これが何を意味するのかはよくわかりません。私たちがやろうとしていることについて、正しい方向に進んでいるかどうかを知りたいと思います。Student.cファイルのコードは次のとおりです。
#include <stdio.h>
#include <stdlib.h>
#include "Student.h"
#include "String.h"
static void error(char *s) {
fprintf(stderr, "%s:%d %s\n", __FILE__, __LINE__, s);
exit(1);
}
extern Student newStudent(char *name, int age, char *major) {
Student s;
if (!(s = (Student) malloc(sizeof(*s))))
error("out of memory");
s->name = name;
s->age = age;
s->major = major;
return s;
}
extern char *studentToString(Student s) {
const int size = 3;
char age[size + 1];
snprintf(age, size, "%d", s->age);
char *line = newString();
line = catString(line, "<");
line = catString(line, s->name);
line = catString(line, " ");
line = catString(line, age);
line = catString(line, " ");
line = catString(line, s->major);
line = catString(line, ">");
return line;
}
*studentToStringメソッドが*toStringメソッドに置き換えられることはわかっていて、*toStringメソッドの内容は*studentToStringメソッドと同じであると考えています。しかし、それがどのようにオブジェクト指向になるのかはわかりません。また、指示から、新しいStudentオブジェクトを作成するときに、新しいtoStringメソッドを指すポインターをnewStudentメソッドに含める必要があることも決定しました。
私たちは私たちのためにプログラムを行う人を探していません。私たちの教授が一週間町を離れていたので、私たちは私たちが何をすべきかを理解したいだけです。どんな助けでも大歓迎です。