-1

I wonder if I can directly convert the public class to a struct. Is it permissible?

  class car
  {
     public:
     int vno;
     float count;
     char  dname[15],x,l[50];
      void input()
      

Is it okay just to convert to struct directly like this?

 struct car
  {
    int vno;
    float count;
    char  dname[15],x,l[50];
    void input()
    

will it be run with no error? Or there is another way...

  • Yes, you can do that. – jkb Jul 03 '22 at 07:26
  • It should be OK. In C++ a `struct` is merely a `class` where all members are public by default. – wohlstad Jul 03 '22 at 07:27
  • They are exactly the same in C++. apart from the default visibility. – tkausl Jul 03 '22 at 07:27
  • Instead of asking vague questions, it's probably better you tell us about the actual problems you faced, when trying to compile your code. – πάντα ῥεῖ Jul 03 '22 at 07:29
  • A good site to use for reference material is cppreference.com (used by many of us), in this case have a look at : [structs and classes](https://en.cppreference.com/book/intro/classes ). More on learning about C++ (online resource) : https://www.learncpp.com/, https://www.learncpp.com/cpp-tutorial/classes-and-class-members/ (it also starts out with structs) – Pepijn Kramer Jul 03 '22 at 07:46
  • This is fine, but if you have any forward declarations anywhere you'll have to change those too. – Paul Sanders Jul 03 '22 at 10:18

1 Answers1

0

A class has private fields by default, with no keywords such as "private" and "public". If you add these attributes, you can manipulate fields' visibilities.

A struct, on the other hand, has public fields by default and we do not have to add keywords of the type public; but we can also have private data in the structure after the private keyword.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Eriss69
  • 69
  • 5