Can I implement a record as a class in programming languages that uses it?

70 views Asked by At

My instructor for "Programing Languages Design & implementation" course asked us to do a homework, and this homework asks that: "can you implement a record as a class ?"

1

There are 1 answers

0
user3437460 On BEST ANSWER

Of course you can. If you are referring a record just like a row of record from the database:

Student Record in database:

Student Name: Alice
Student ID: S001
Gender: F

Student in Struct (Implemented in C)

struct Student
{
    char name[MAX];
    char id[MAX];
    char gender;
};
struct Student stud1 = {"Alice", "S001", 'F'};

Student in Class (C has no class, implemented in Java)

class Student
{
    String name;
    String id;
    char gender;

    public Student (String name, String id, char gender)
    {        
        this.name = name;
        this.id = id;
        this.gender = gendeer;
    }
}
Student stud1 = new Student1("Alice", "S001", 'F');

If the context of C++, the same thing applies. In C++, you can have struct and classes. Struct cannot have method (functions) in them, but classes can have. Since classes can similarly "group" data together, and on top of that, provide methods. You can definitely implement a struct as a class.