Using an object in several .cpp files

I am creating a Library Management system. And Im having a problem.

I have a Main.cpp, Book.cpp, DVD.cpp and Student.cpp. I am creating a book and dvd object in the Main.cpp and also a student object in the Student.cpp. I am trying to use the dvd, book and student objects in the Book.cpp and DVD.cpp but unsure how to do it.

I have included some of the code from each of the .cpp files showing the use of the objects.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
            switch(dvdOption)
            {
                case '1':
                    dvd2.issueDVD();
                    processed = true;
                    break;
                case '2':
                    dvd2.returnDVD();
                    processed = true;
                    break;
                case '3':
                    dvd2.insertDVD();
                    processed = true;
                    break;
                case '4':
                    dvd2.updateDVD();
                    processed = true;
                    break;
                case '5':
                    dvd2.deleteDVD();
                    processed = true;
                    break;
                case '6':
                    char barcode[6];
                    cout<<"Enter The DVD barcode: " <<endl;
                    cin>>barcode;
                    dvd2.searchDVD(barcode);
                    processed = true;
                    break;
                case '7':
                    dvd2.showallDVDs();
                    processed = true;
                    break;
                case '8':
                    processed = true;
                    break;
                case '9':
                    exit(0);
                    break;
                case '10':
                    default:
                    cout<<"Incorrect selection. Please select from the given options." <<endl;
                    processed = false;
                    break;
            }


DVD.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    void DVD::issueDVD()
    {
        fstream file,file1;
        char studentno[6],bookno[6];
        int found=0,sys=0;
        
        cout<<"Enter the Registration no. of the Student: "<<endl;
        cin>>studentno;
        file.open("student.dat",ios::in|ios::out);
        file1.open("book.dat",ios::in|ios::out);
        while(file.read((char*)&student3,sizeof(Student)) && found==0)
        {
            if(strcmp(student3.retregistrationNo(),studentno)==0)
            {
                found=1;
                cout<<"Enter barcode of the Book you want to issue: "<<endl;
                cin>>bookno;
                while(file1.read((char*)&dvd1,sizeof(DVD))&& sys==0)
                {
                    if(strcmp(retBarcode(),bookno)==0)
                    {
                        displayDVD();
                        sys=1;
                        student3.getstudentbookBar(retBarcode());
                        long pos=-1*sizeof(student3);
                        file.seekp(pos,ios::cur);
                        file.write((char*)&student3,sizeof(Student));
                        cout<<"Book issued";
                    }
                }
                if(sys==0)
                        cout<<"Error: Book barcode does not exist. "<<endl;
            }
                
        }
        
        if(found==0)
            cout<<"Error: Student does not exist in the system. "<<endl;
        
        file.close();
        file1.close();
    }


Book.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
    void Book::updateBook()
    {
        fstream file;
        int found=0;
    	char barcode[6];
    	cout<<"Enter barcode of the Book you want to update:" <<endl;
    	cin>>barcode;
    	file.open("book.dat",ios::in|ios::out);
    	while(file.read((char*)&book1,sizeof(Book)) && found==0)
    	{
    		if(strcmp(retBarcode(),barcode)==0)
    		{
                displayBook();
                editBook();
    			long pos=-1*sizeof(book1);
    		    file.seekp(pos,ios::cur);
    		    file.write((char*)&book1,sizeof(Book));
    		    cout<<"Book updated"<<endl;
    		    found=1;
    		}
    	}
    
        	file.close();
        	if(found==0)
        		cout<<"Error:Book Not Found. " <<endl;
    
    }


For using the class or function declared in a .cpp (or .h) you must include his header file. So if for instance you want to use the book class, you must include his header file (Book.h, assuming that this is his name).
Topic archived. No new replies allowed.