0

I am simply making a program using inheritance and I am trying to call the functions that I created in my derived classes. I am receiving this error when I am running my code "identifier "searchCourse" is undefined" as well as ""searchCourse" was not declared in this scope" To my knowledge this is how you call a function in C++ and I am stuck. Any help is appreciated.

#include <iostream>

using namespace std;
class User
{
protected:
    string firstName;
    string lastName;
    string idNumber; //attributes for user class

public:
    virtual void print()
    {
        cout << firstName + lastName << "has ID number " << idNumber << ".\n";  //virtual function to print all info about user
    }
    User(string name1, string name2, string ID) //constructor for base class when user is created
    {
        firstName = name1;
        lastName = name2;
        idNumber = ID;
    }
    ~User() {}

    string getFirst() { return firstName; }
    string getLast() { return lastName; }
    string getID() { return idNumber; }
};

class Student : public User
{
    protected:

    public:
        Student(string name1, string name2, string ID) :User(name1, name2, ID) {}
        ~Student(){}
        
        void searchCourse()
        {
            User::print();
            cout << "Searching Courses";
        }
        void addDrop()
        {
            User::print();
            cout << "Add Dropping courses"; 
        }
        void printSchedule()
        {
            User::print();
            cout << "Printing Schedule";
        }
};

class Instructor : public User
{
    protected:

    public:
        Instructor(string name1, string name2, string ID) :User(name1, name2, ID) {}
        ~Instructor(){}
        
        void printSchedule()
        {
            User::print();
            cout << "Printing Schedule";
        }
        void printRoster()
        {
            User::print();
            cout << "Printing Class List";
        }
        void classSearch()
        {
            User::print();
            cout << "searching for course"; 
        }
};        

class Admin : public User
{
    protected:

    public:
        Admin(string name1, string name2, string ID) :User(name1, name2, ID) {}
        ~Admin(){}
        
        void addCourse()
        {
            User::print();
            cout << "Adding Course";
        }
        void removeCourse()
        {
            User::print();
            cout << "Removing Course";
        }
        void addRemoveUser()
        {
            User::print();
            cout << "Adding/Removing user"; 
        }
        void addRemoveStudent()
        {
            User::print();
            cout << "Adding/Removing Student from Course";
        }
        void SearchPrint()
        {
            User::print();
            cout << "Searching and Printing";
        }
};
int main()
{
    searchCourse();
} 
   
  • 2
    `searchCourse()` is a `public` member of `Student` class. You have to create object of `Student` class and call `searchCourse()` using that object. – H.S. May 23 '21 at 04:14
  • I got it to create an object of student but now it says searchCourse was not declared in this scope – Hamza Khalid May 23 '21 at 04:27
  • 1) You forgot `#include `. 2) The `User` class should have a `virtual` destructor. – PaulMcKenzie May 23 '21 at 04:46

1 Answers1

0

You need to create student object and invoke the function from it. Use below main function

Student *s = new Student("","","");
s->searchCourse();
Dilawar
  • 5,438
  • 9
  • 45
  • 58
Vinayak Singla
  • 142
  • 1
  • 12