samedi 27 février 2021

Inheriting a variable from a class

I am working on a project which has an abstract class A and several classes, suppose class B, C, D which all inherit publicly from class A.

Class A has a bunch of getters and setters as well as a few private member variables.

One of these member variables is a string called name.

Another one of these member variables is a character called abbreviation, which holds the first character of the name string.

How can I go about setting the string and character variables without needing getters and setters to name them in the code of functions I write. I do not want to have to set them at the start of the program run.

Is it possible to set them in their constructors? For example:

// B constructor
B::B() {
name = "B is the name of this variable";
abbreviation = 'B';
}

Thanks in advance ! :D

Aucun commentaire:

Enregistrer un commentaire