ICantCode
ICantCode

Reputation: 35

Multiple Class Files C++

I am unsure about the use of separate files for classes. How do I make functions inside the classes? Where do I put it?

QuizMain.cpp:

#include "QuizMain.h"
#include <iostream>
#include <string>

using namespace std;

QuizMain::QuizMain()
{
    // Hia stackoverflow
}

QuizMain.h file:

#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>

using namespace std;

class QuizMain
{
    public:
        QuizMain();
    private:
};

#endif // QUIZMAIN_H

Main file:

#include <iostream>
#include <string>
#include "QuizMain.h"

using namespace std;

int main()
{
    QuizMain qm;

    return 0;
}

How would I make a class and call it correctly?

Upvotes: 0

Views: 5802

Answers (3)

Shakiba Moshiri
Shakiba Moshiri

Reputation: 23794

It is easy to do.
If you use the IDE project, the IDE set you to use the any file. Like code::block IDE But if you do not use the IDE project it is a little different to use.
You should write the .h file and then,after all writing you should put .cpp file.
Also you can use interface class that works by poiter.


/// .h file and declaration 
#ifndef ONE.H
#define ONE.H

 class one {
public:
    one();
    ~one();
 };
#include "one.cpp"
#endif ONE.H

then:

/// .cpp file and implementation 
one::one(){
    std::cout<<"constructor one"<<std::endl;
}
one::~one(){
    std::cout<<"destructor one"<<std::endl;
}

then :

#include <iostream>
#include "one.h"
int main()
{
      one o;
}

output:

constructor one
destructor one

Process returned 0 (0x0)   execution time : 0.010 s
Press ENTER to continue.

Upvotes: 0

Humam Helfawi
Humam Helfawi

Reputation: 20264

This is an example:

QuizMain.cpp file:

#include "QuizMain.h"
#include <iostream>
#include <string>

using namespace std;

QuizMain::QuizMain()
{
    // Hia stackoverflow
}

void QuizMain::my_new_function(std::string my_name){
    std::cout << "Hi " + my_name +"!" << std::endl;
}

QuizMain.h file:

#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>

class QuizMain
{
    public:
        QuizMain();
        void my_new_function(std::string my_name);
    private:
};

#endif // QUIZMAIN_H

Main file:

#include <iostream>
#include <string>
#include "QuizMain.h"

using namespace std;

int main()
{
    QuizMain qm;
    qm.my_new_function("foo");
    return 0;
}

Anyway, there is no point from asking such a question here. You should probably find a good book/resource and learn how to write and use functions.

Upvotes: 4

sebenalern
sebenalern

Reputation: 2561

Normally you have a header file and cpp file. The header file is where you declare your functions and member variables. The cpp file is where you implement your functions.

quizmain.h

// QuizMain.h file
#ifndef QUIZMAIN_H
#define QUIZMAIN_H
#include <string>

    class QuizMain
    {
        public:
            QuizMain(int quizScore);
            // declare public functions here
        private:
           int quizScore; // declare private member variables here.
    };

    #endif // QUIZMAIN_H

cpp file

// QuizMain.cpp file
#include "QuizMain.h"
#include <iostream>
#include <string>

using namespace std;

QuizMain::QuizMain(int quizScore)
{
    this.quizScore = quizScore; // init a quiz score
}

main

Call and create a class object like this

QuizMain quiz(95);

Upvotes: 1

Related Questions