We were given an assignment in our C programming class to modify a program to make it more object oriented. Part of this was to fix the toString method. The directions were:
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)
However, we aren't really sure what this means and would like to know if we are on the right track with what we are suppose to do. Here is the code from the Student.c file:
#include <stdio.h>
#include <stdlib.h>
#include 开发者_Go百科"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;
}
We known that the *studentToString method will be replaced by a *toString method and we think that the *toString method will have the same contents as the *studentToString method. But we don't understand how that makes it more object-oriented. We have also determined, from the directions, that when we create a new Student object, we should have a pointer in the newStudent method that points to the new toString method.
We're not looking for anyone to do the program for us. We just want to understand what we are suppose to do as our professor has been out of town for the week. Any help would be greatly appreciated.
It sounds like he's asking you to take the Student structure and add a pointer to a function inside the structure itself so that when you have a valid pointer to a Student structure you can do something like
`myStudent->toString();`
and have it return the same value as
`studentToString(myStudent)`
would have before. This makes it more object oriented because of the fact that you're calling a toString
method on an effective "instance" (for lack of better terms) of a Student
structure and returning parameters related to that "instance." Just as you would in some sort of object-based programming language.
My guess is that you need to add a member to the Student struct, the type of that member would be a function pointer.
Then define that function.
Then add a parameter taking a function pointer to newStudent.
Then set that newly created member to the value of the parameter.
(this feels like an extremely abstract way to learn OO, but that's just my opinion)
Looks like your prof set you this problem so that you get an understanding of polymorphism. In this example, the idea is that every object in your system should have its own way of rendering itself as a string but you don't want to know the details; you just want to be able to call toString
on any object.
E.g.
banana->toString()
apple->toString()
精彩评论