简体   繁体   中英

interface and implementation C++

I'm writing a code to separate interface and it's implementation but it has an error, I followed the correct syntax of interfaces and classes ,I have seen many examples on this subject, Knowing I'm a former C# programmer.

code:

#include "stdafx.h"
#include<iostream>
#include <String>
using namespace std;

            ///////////////////EMPLOYEE CLASS AND INTERFACE //////////////
class Interface_Employee
{

public:
    Interface_Employee() {}
    virtual ~Interface_Employee() {}
    virtual void print() = 0;    // "= 0" part makes this method pure virtual, and
                                 // also makes this class abstract.
};

class Employee : public Interface_Employee
{
    int ID, Age;
    Bank ac1, ac2; //composition
public:
    Employee(int a, int b, Bank account1, Bank account2)
        : ac1(account1), ac2(account2)
    {
        ID = a; Age = b;
    }
    ~Employee() {}


    // Provide implementation for the first method

    void print()
    {
        cout << "Age is:/n" << Age << "ID is:" << ID;
        ac1.print();
        ac2.print();
    }
};
                 ////////////BANK CLASS AND INTERFACE//////////////////


      class Interface_Bank
      {
          public:
          Interface_Bank() {}
          virtual ~Interface_Bank() {}
          virtual void print() = 0;    // "= 0" part makes this method pure virtual, and
                               // also makes this class abstract.
      };

      class Bank : public Interface_Bank
      {
        private:
        int balance, ID;

        public:
        Bank(int a, int b)
        {
          balance = a;
          ID = b;
        }
        ~Bank() {}

     // Provide implementation for the first method
        void print()
            {
              cout << "Balance is:/n" << balance << "ID is:" << ID;
            }


           };

        class Output {

           int main(void)
            {
            Bank a(1000, 1);
            Bank b(2000, 2);
            Employee c(2, 25, a, b);

            return 0;
           }

          };

Error:

Severity Code Description Project File Line Suppression State Error C3646 'ac1': unknown override specifier console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 22
Error C2059 syntax error: ',' console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 22
Error C2238 unexpected token(s) preceding ';' console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 22
Error C2061 syntax error: identifier 'Bank' console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 24
Error C2065 'account1': undeclared identifier console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 25
Error C2065 'account2': undeclared identifier console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 25
Error C2614 'Employee': illegal member initialization: 'ac1' is not a base or member console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 25
Error C2614 'Employee': illegal member initialization: 'ac2' is not a base or member console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 25
Error C2065 'ac1': undeclared identifier console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 37
Error C2228 left of '.print' must have class/struct/union console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 37
Error C2065 'ac2': undeclared identifier console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 38
Error C2228 left of '.print' must have class/struct/union console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 38
Error C2661 'Employee::Employee': no overloaded function takes 4 arguments console_App c:\\users\\dell_alrfou\\source\\repos\\console_app\\console_app\\console_app.cpp 81

You need to declare or define the Bank class before you attempt to use it.

You need to move the definition of Bank (and its Interface_Bank base class) to before class Employee .

  1. Before use Bank , you should declare or define it
  2. int main() shouldn't in a class

The following code could work:

#include<iostream>
#include <String>
using namespace std;

class Interface_Bank
{
public:
    Interface_Bank() {}
    virtual ~Interface_Bank() {}
    virtual void print() = 0;
};

class Bank : public Interface_Bank
{
private:
    int balance, ID;

public:
    Bank(int a, int b) : balance(a), ID(b)
    {
    }
    ~Bank() {}

    void print()
    {
        cout << "Balance is:/n" << balance << "ID is:" << ID;
    }
};

class Interface_Employee
{
public:
    Interface_Employee() {}
    virtual ~Interface_Employee() {}
    virtual void print() = 0;
};

class Employee : public Interface_Employee
{
private: 
    int ID, Age;
    Bank ac1, ac2;
public:
    Employee(int a, int b, Bank account1, Bank account2)
        : ID(a), Age(b), ac1(account1), ac2(account2)
    {
    }
    ~Employee() {}

    void print()
    {
        cout << "Age is:/n" << Age << "ID is:" << ID;
        ac1.print();
        ac2.print();
    }
};

int main(void)
{   
    Bank a(1000, 1);
    Bank b(2000, 2);
    Employee c(2, 25, a, b);
    c.print();
    return 0;
}

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