简体   繁体   中英

Writing classes in different file in c++

I am writing a c++ program which consists of 8 classes.

1.Vehicle
      derived class 1. Seat
                    2.Door 
2.Passenger
3.Settings
      derived class 1. SeatSettings
                    2. InfotainmentSettings
4.PassengerLocation  

I don't have any previous experience in c++. While writing the program should I write all the classes in a single .cpp file or in different .cpp file. If as different .cpp file then parent and derived class should be in same file ?.

Really you can write classes in any place of your programs. Inside h files, cpp files, inside other classes , inside methods and so on. C++ give complete control under source code of the application.

In general way you describe interface of a class in a h files, and implement methods in a cpp file. But you can implement inline or template methods in the h file.

I think that you should start from Classes guide , and when you will have knowledges about capability you can select right way to implement your classes.

除非是模板类或结构,否则应为在c ++中实现的每个类分别创建一个.h和.cpp文件对。

Separate the classes but keep the parent and derived classes in the same file. Put your class declarations in .h files and the implementations in .cpp files and then include the .h file in its corresponding .cpp file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM