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 8354251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:29:46+00:00 2026-06-09T09:29:46+00:00

How do I separate classes into multiple files? Here is my understanding so far:

  • 0

How do I separate classes into multiple files? Here is my understanding so far:

  1. Create new class, and a ".h" and a ".cpp" file for it.
  2. You use #include classname.h in your main source file to import its contents.
  3. The Classname::Classname at the beginning of the source file is a Scope Resolution Operator.
  4. You can call functions from main by using the objects that you have declared.

I’m just confused about how to implement this in practice. I have created a working calculator program with all the classes in one source file:

#include <iostream>
using namespace std;

class Addition {
    public:
    float add(float x, float y) {
        float sum;
        sum = x + y;
        return sum;
    }
};

class Subtraction {
    public:
    float subtract(float x, float y) {
        float dif;
        dif = x - y;
        return dif;
    }
};

class Multiplication {
    public:
    float multiply(float x, float y) {
        float prod;
        prod = x * y;
        return prod;
    }
};

class Division {
    public:
    float divide(float x, float y) {
        float quot;
        quot = x / y;
        return quot;
    }
};

int op;
char cont;

int main() {
    do {
    cout << "Welcome to C++ Calculator v2!" << endl;
    cout << "Select the number for which operation you want to use: " << endl;
    cout << "1-Addition" << endl;
    cout << "2-Subtraction" << endl;
    cout << "3-Mutliplication" << endl;
    cout << "4-Division" << endl;
    cin >> op;

    if (op == 1) {
        float num1;
        float num2;
        Addition addObj;
        cout << "You have chosen Addition!" << endl;
        cout << "Enter the first number you want to add: " << endl;
        cin >> num1;
        cout << "Enter the second number you wat to add: " << endl;
        cin >> num2;
        float ans = addObj.add(num1, num2);
        cout << "The sum is " << ans << endl;
        cout << "Do you wish to continue? Y/N" << endl;
        cin >> cont;
    }

    if (op == 2) {
        float num1;
        float num2;
        Subtraction subObj;
        cout << "You have chosen Subtraction!" << endl;
        cout << "Enter the first number you want to subtract: " << endl;
        cin >> num1;
        cout << "Enter the second number you want to subtract: " << endl;
        cin >> num2;
        float ans = subObj.subtract(num1, num2);
        cout << "The difference is " << ans << endl;
        cout << "Do you wish to continue? Y/N" << endl;
        cin >> cont;
    }

    if (op == 3) {
        float num1;
        float num2;
        Multiplication multObj;
        cout << "You have chosen Multiplication!" << endl;
        cout << "Enter the first number you want to multiply: " << endl;
        cin >> num1;
        cout << "Enter the second number you want to multiply: " << endl;
        cin >> num2;
        float ans = multObj.multiply(num1, num2);
        cout << "The product is " << ans << endl;
        cout << "Do you wish to continue? Y/N" << endl;
        cin >> cont;
    }

    if (op == 4) {
        float num1;
        float num2;
        Division divObj;
        cout << "You have chosen Division!" << endl;
        cout << "Enter the first number you want to divide: " << endl;
        cin >> num1;
        cout << "Enter the second number you want to divide: " << endl;
        cin >> num2;
        float ans = divObj.divide(num1, num2);
        cout << "The quotient is " << ans << endl;
        cout << "Do you wish to continue? Y/N" << endl;
        cin >> cont;
    }

    } while (cont == 'Y' || cont == 'y');

    if (cont == 'N' || 'n') {
    cout << "Thanks for using my program, goodbye!" << endl;
    }

    return 0;
}

I know there is a lot easier way to make something like this, but I’ve used classes and objects instead for the sole purpose of practice.

  • 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-06-09T09:29:48+00:00Added an answer on June 9, 2026 at 9:29 am

    Ok, I will show you by doing your example:

    subtraction.h

    class Subtraction 
    {
    public:
     float subtract (float x, float y);
    };
    

    subtraction.cxx

    #include "subtraction.h"
    
    float Subtraction::subtract (float x, float y)
    {     
      float dif;
      dif=x-y;
      return dif;    
    }
    

    multiplication.h

    class Multiplication 
    {
    public:
      float multiply (float x, float y);
    };
    

    multiplication.cxx

    #include "multiplication.h"
    
    float Multiplication::multiply (float x, float y)
    {
      float prod;
      prod=x*y;
      return prod;
    }
    

    and so on…

    main.cxx

    #include "subtraction.h"
    #include "multiplication.h"
    
    int main()
    {
     //use the classes just as before.
    }
    

    Also, I didn’t put it in the code here, for simplicity, but go ahead and get into the habit of ensuring that your declarations are only included once. On a large project, this can get very nasty if you don’t put these safeguards in.

    #ifndef SUBTRACTION_H
    #define SUBTRACTION_H
    
    class Subtraction
    {
         ....
    };
    #endif /*SUBTRACTION_H*/
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two separate classes, A and B. I also have Repository class
Here is what I'm looking for: I'd like to separate pieces of functionality into
I am trying to separate a large table into multiple discrete types. I'm following
I have a class file that contains all the classes that are needed for
Alright so I have been trying to get all of my separate classes (At
I have an interface that I want to implement in separate classes after doing
Lets put the case that we've got two separate classes, each owns a mysqli
I'd like to keep my concrete classes separate from my views. Without using strongly
Put them all in one separate folder structure or along with classes which implements
I am using a set of QWizardPage classes within a QWizard. Separate to this

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.