-1

I always see people asking how you convert from a structure to a class but never the other way around. Is it because it's much simpler or is it for another reason? Let's say you have a department class like the one below:

class Department {
    private:
        int DepartmentID;
        string Departmentname;
        string DepartmentHeadName;
    public:
    Departments() {
            DepartmentID = 0;
            Departmentname = "";
            DepartmentHeadName = "";
    }
    Departments(int di, string dn, string dhn)
    {
        DepartmentID = di;
        Departmentname = dn;
        DepartmentHeadName = dhn;
    }
    void setdepartmentid(int di) {
        DepartmentID = di;
    }
    int getdepartmentid() {
        return DepartmentID;
    }
    void setDepartmentName(string dn) {
        Departmentname = dn;
    }
    string getDepartmentName() {
        return Departmentname;
    }
    void setDepartmentHeadname(string dhn) {
        DepartmentHeadName = dhn;
    }
    string getDepartmentHeadname() {
        return DepartmentHeadName;
    }

};

And you were asked to "Replace class with structure for Department." How would you approach this? Would you simply remove class and replace it with struct or would you do/try something else?

dbush
  • 186,650
  • 20
  • 189
  • 240
RUBYXCUBE
  • 1
  • 1
  • 3
    I mean, you could change the keyword `class` to `struct` and things would still compile fine, but that doesn't really do anything. What are you trying to accomplish here? – Silvio Mayolo Jul 19 '18 at 01:45
  • 2
    `struct` and `class` are exactly the same except `struct` defaults to public access and `class` defaults to private access. – Retired Ninja Jul 19 '18 at 02:14
  • 3
    Possible duplicate of [What are the differences between struct and class in C++?](https://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c) – Retired Ninja Jul 19 '18 at 02:15

3 Answers3

1

To convert c++ class to structure:-

Step 1: Make sure that you provide access modifiers to all members of class explicitly.

Step 2: Replace class with struct.

And you are done.

The only difference between struct and class in c++ is that by default all the members of a class is private whereas all the members of a structure is public. That is why before converting it is important to provide access modifiers explicitly.

Honey Yadav
  • 166
  • 12
0

I think this might be what you need:

C++ Primer § 7.2

A class may define members before the first access specifier. Access to such members depends on how the class is defined. If we use the struct keyword, the members defined before the first access specifier are public; if we use class, then the members are private.

As a matter of programming style, when we define a class intending for all of its members to be public, we use struct. If we intend to have private members, then we use class.

The only difference between using class and using struct to define a class is the default access level.

class Foo{
    void functionA(){
        //this is a private member function
    }
    int i = 3;  //private 
public:
    int j = 4;   //public
    void functionB(){  //public

    }
};

struct Foo{
int j =4;  //public
void functionB(){  //public

}
private:
    void functionA(){
        //to keep it priviate under a struct, you need to specify the access specifier explicitly
    }
    int i = 3; //private
};
Rick
  • 6,046
  • 2
  • 37
  • 65
-1

Yes, quit class and put struct. C++ structs are very different to C structs. C structs are merely aggregates of raw data in the RAM. C++ struct is (almost) the same as C++ class. So yes, if you want to convert a class into a struct, you only need to change class for struct.

[EDIT]: As suggested by @Gox, I must add that there is a tiny but important difference: the default level access. In a class the default level access is private, unless you explicitly specify other. In a struct is the opposite as all is public by default.

  • 2
    Patently false. structs allow inheritance just fine. – Silvio Mayolo Jul 19 '18 at 01:55
  • 2
    "*C++ structs are classes that don't allows inheritance*" Huh? https://godbolt.org/g/Ytn273 –  Jul 19 '18 at 01:56
  • What??? It allows it?. OMG! my teacher sucks. I will check it. –  Jul 19 '18 at 01:58
  • 3
    There are sadly a lot of bad C++ teachers out there. Mostly Java or C# programmers who pretend to know C++. Try asking them if they know what an rvalue reference is, or what `std::optional` is. I've had C++ teachers who don't even know what a reference is. – Silvio Mayolo Jul 19 '18 at 01:58
  • Checked. Patently he (my teacher) is wrong. I will enjoy this tomorrow, showing this post to him. Now I will edit my answer. Thanks, true teachers! –  Jul 19 '18 at 02:01
  • Done. Look, it's even shorter and simpler. –  Jul 19 '18 at 02:02
  • you got upvote from me but add thos to your answer in claa everything is private by default where as struct it is opposite –  Jul 19 '18 at 04:29