Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 679141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:13:37+00:00 2026-05-14T01:13:37+00:00

In C# or Java, classes are declared and defined at the same time. In

  • 0

In C# or Java, classes are declared and defined at the same time. In C++, the norm is to do that separately. What if we write the whole class in one , say .cpp, file and include that in files that references to it, what kinds of bad thing technically would happen besides a lengthened compilation process?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T01:13:38+00:00Added an answer on May 14, 2026 at 1:13 am

    If your implementation of MyClass is all in the header file MyClass.h then any file you needed to implement MyClass will be included whenever someone includes MyClass.h.

    If you change any part of MyClass.h, even if it’s trivial (such as adding a comment or even a space) then all files that include it will have to recompile, even if the interface hasn’t changed.

    Neither of these matters for toy projects, but as you noted, when you have a program that consists of hundreds (or thousands, etc.) of class files, the added compilation time alone makes it worthwhile to separate out implementation from interface.

    For instance, if I have the following:

    // MyClass.h
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    
    #include "Inventory.h"
    
    class MyClass
    {
    public:
      MyClass();
    
      void processInventory(Inventory& inventory)
      {
        // Do something with each item in the inventory here
        // that uses iostream, iomanip, sstream, and string
      }
    private:
      // ...
    };
    

    It would more ideomatically be written as:

    // MyClass.h
    class Inventory;
    
    class MyClass
    {
    public:
      MyClass();
    
      void processInventory(Inventory& inventory);
    private:
      // ...
    };
    
    // MyClass.cc
    #include "MyClass.h"
    
    #include <iostream>
    #include <iomanip>
    #include <sstream>
    #include <string>
    
    #include "Inventory.h"
    
    MyClass()::MyClass()
    {
    }
    
    void MyClass()::processInventory(Inventory& inventory)
    {
      // Do something with each item in the inventory here
      // that uses iostream, iomanip, sstream, and string
    }
    

    Notice: Including MyClass.h doesn’t mean iostream, iomanip, sstream, string, or Inventory.h have to be parsed. Changing how processInventory works doesn’t mean all files using MyClass.h have to be recompiled.

    Notice how much easier it can be to figure out how to use MyClass now. Header files serve an important purpose: they show people how to use your class. With the modified MyClass.h it’s easy to see the list of functions. If each function is defined in the header, then you can’t look at just the list of functions. That makes it harder to figure out how to use the class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two Java classes giveMyOb and dataConn declared in the same directory. Both
If one writes two public Java classes with the same case-insensitive name in different
I have two java classes as follows App1 without a package: class App1 {
I have some java classes like, Class User { String email; String name; String
In 2 java classes , If class A implements Serializable and class B extends
I have a Java program that generates Java classes for my application. Basically it
I have a huge java codebase (more than 10,000 java classes) that makes extensive
I have an abstract base class that many classes extend. I'd like all these
I'm trying to determine how many classes are declared in a .java file, using
I have a Java problem with nested classes. My first class structure looked like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.