Detach header file during c++ override

Asked 2 years ago, Updated 2 years ago, 106 views

I'd like to divide the header file into three classes that receive the override, one main class, and the main function. But maybe because of virtual, I don't know the criteria for dividing the header file, and it runs somehow, but it can only be calculated by putting all the virtual functions in the header file. Could you tell me how to divide the header file?

#ifdef virtual
#include <iostream>
using namespace std;

#include "ice1.h"
#include "Icecream.h"

ice1 :: virtual void icecream(int left) { }
ice1 :: virtual void show() { }
ice1 :: virtual void count(int left) { }
ice1 :: virtual int all(int left) { return sum; }


virtual void icecream(int left) {
    if (ice_count >= left) {
        ice_count -= left;
        sell += left;
        sum = sell * price1;
        cout << "I purchased it." << endl;
    }
    else
        cout << "The number is insufficient to purchase." <<endl;
}
virtual void count(int left) {
    if (ice_count >= left || ice_count >= 0) {
        cout << "Total sales amount of Worldcon: "<< sum << "Won" << endl;
        cout << "---------------\n" << endl;
    }
    else {
        cout << "Total sales amount of Worldcon: "<< sum << "Won" << endl;
        cout << "---------------\n";
    }
}
virtual int all(int left) {
    return sum;
}

#endif
#pragma once
#ifndef ICE1_H
#define ICE1_H
#include <string>
#include <iostream>
#include "Icecream.h"

using namespace std;

class ice1 : public Icecream {
    int price1;
public:
    ice1(string in, int ice_c, int price_1) : Icecream(in, ice_c) { price1 = price_1; }
    virtual void show() {
        "Cout << icename << ice_count <<" Price << < < price1 <<" < < < < price1 << <" Won"
    }
};

#endif 
#ifdef virtual
#include<iostream>
using namespace std;

#include "Icecream.h"

Icecream :: virtual void icecream(int left) { }
Icecream :: virtual void show() { }
Icecream :: virtual void count(int left) { }
Icecream :: virtual int all(int left) { return sum; }

#endif
#pragmaence
#ifndef ICECREAM_H
#define ICECREAM_H
#include <string>
#include <iostream>

using namespace std;

class Icecream {
protected:
    string icename;
    int ice_count, sum = 0, sell = 0;
public:
    Icecream(string in, int ice_c) { icename = in; ice_count = ice_c; }
    virtual void icecream(int left) { }
    virtual void show() { }
    virtual void count(int left) { }
    virtual int all(int left) { return sum; }
};
#endif
#include<iostream>
using namespace std;

#include "ice1.h"
#include "ice2.h"
#include "ice3.h"
#include "Icecream.h"

int main() {
    int ice_num, left;
    string choice;
    Icecream* p[3];
    p[0] = news1 ("Worldcon", 10, 1000);
    p[1] = news2 ("Wow", 10, 1200);
    p[2] = news3 ("Pig Bar", 10, 700);

    Cout << "Three ice creams are as follows. \n";
    cout << "Ice cream 1: "; p[0]-> show();
    cout << "Ice cream 2: "; p[1]-> show();
    cout << "Ice cream 3:"; p[2]-> show();
    cout << endl;

    while (true) {
        cout << "Enter ice cream (1: Worldcon, 2: Wow, 3: Pig bar) and number>>";
        cin >> ice_num >> left;
        if (ice_num == 1) {
            p[ice_num - 1]->icecream(left);
            p[ice_num - 1]->count(left);
            p[0]->show();
            p[1]->show();
            p[2]->show();
        }
        else if (ice_num == 2) {

            p[ice_num - 1]->icecream(left);
            p[ice_num - 1]->count(left);
            p[0]->show();
            p[1]->show();
            p[2]->show();
        }
        else if (ice_num == 3) {
            p[ice_num - 1]->icecream(left);
            p[ice_num - 1]->count(left);
            p[0]->show();
            p[1]->show();
            p[2]->show();
        }
        else
            cout << "You entered it incorrectly.\n";

        cout << "Do you want to continue purchasing (y/n)>>";
        cin >> choice;
        if (choice != "y") {
            cout << "\n";
            cout << "Sales \n--------- \n";
            cout << "Total sales of ice cream today: " <<p[0]->all(left) + p[1]->all(left) + p[2]->all(left);
            break;
        }
        cout << endl;
    }
}

virtual header-files

2022-09-20 14:23

2 Answers

The virtual keyword is entered only in the member function declaration within class. In the definition of a member function, do not enter virtual. The member function definition must be created as follows:

returnableclass full name::function name(parameter list) { ...}

accordingly ice1::virtual void show(){} must be written as voidice1::show(){}.

For C++ questions, please put c++ in the question tag.


2022-09-20 14:23

You must be able to distinguish between declaration and definition when using classes, functions, etc. in C++.

For example, an undefine error occurs if the function definition is below the function call statement that calls the sum function in the main function as shown below.


int sum(int, int); // <- function circularity = function decay

int main(void)
{
    int x = 100;

    int y = sum(x, 50);
    printf("sum: %d\n", y);

    return 0;
}

int sum(inta, intb) // <- Function definition = function definition
{
    return a+b;
}

This is because the compiler does not have the necessary information when parsing int y = sum(x, 50); to prevent this, you must specify a function prototype above the main function, such as int(int, int).

The function prototype corresponds to the declaration of the function.

With this principle, when separating the header file (.hor.hpp) and the source file (.cor.cpp), you can put the declaration part in the header file and the definition part in the source file.

The virtual syntax belongs to the definition grammar, so you should not enter the definition part.

However, from the source below

Icecream(string in, int ice_c) { icename = in; ice_count = ice_c; }

You can also define at the same time as declaration.

If the part enclosed by {} goes in, it is a phrase that contains definition. (Usually referred to as an inline function)


#pragma once
#ifndef ICECREAM_H
#define ICECREAM_H
#include <string>
#include <iostream>

using namespace std;

class Icecream {
protected:
    string icename;
    int ice_count, sum = 0, sell = 0;
public:
    Icecream(string in, int ice_c) { icename = in; ice_count = ice_c; }
    virtual void icecream(int left) { }
    virtual void show() { }
    virtual void count(int left) { }
    virtual int all(int left) { return sum; }
};
#endif

And this is a side note

Since pragmaonce and ifndef~endif syntax play the same role, You can use one of the two.

When this header file is included elsewhere, it means that even if it is included several times, it will be preprocessed only once.

(Simple would be pragmaonce, right?)


#pragma once
#ifndef ICECREAM_H
#define ICECREAM_H
// ...
// ...
#endif


2022-09-20 14:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.