Defining Classes and using them inside void main()

Hey guys..this is a program I developed in which we had to define a class named BOOK with the data members and member functions as shown in the program..We have to:
(i) Make the user enter the values in the array BOOK.
(ii) Display the details that the user entered.
(iii) Search for a book from the array upon its Bno and display its details.
(iv) Search for a book from the array upon its Bname and display its details.

PROGRAM:
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

class BOOK
{
  private:
	  int Bno;
		  char Bname[20];
		  char Author[20];
		  float Price;
  public:
	      void Enter()
		  {
			  cout<<"Enter Book Number: ";
			  cin>>Bno;
			  cout<<"Enter Book's Name: ";
			  gets(Bname);
			  cout<<"Enter Book's Author: ";
			  gets(Author);
			  cout<<"Enter Book's price: ";
			  cin>>Price;
		  }


		  int RBno()
		  {return Bno;}


		  char* RBname()
		  {return Bname;}

};



void BOOK::Display(BOOK A[], int N)
		  {

			  for(int i=0; i<N; i++)
			  {
			   cout<<"The details of Book "<<i+1<<" are:\n\n";
			   cout<<"Book Number: "<<A[i].Bno<<"\n";
			   cout<<"Name: "<<A[i].Bname<<"\n";
			   cout<<"Author: "<<A[i].Author<<"\n";
			   cout<<"Price: "<<A[i].Price<<"\n";
			   cout<<"\n\n";
			  }


		  }

void main()
{
	clrscr();
	int n, choice, x, found1=0, found2=0; char Input[20];
    BOOK A[20];
	do
	{
	  cout<<"Enter the number of books you'd be entering <=20: ";
	  cin>>n;
	}while(n>20);
    for(int i=0; i<n ; i++)
	{
		A[i].Enter();
	cout<<"\n\n";
	}
	Display(A,n);
	cout<<"There are 2 ways to search a book:\n";
	cout<<"1. By entering the Book Number\n";
	cout<<"2. By entering the Book Name\n";
	cout<<"Enter your choice: ";
	cin>>choice;
	switch(choice)
	{

	case 1:
		{

		   cout<<"Enter the book number of the book you are searching for: ";
	       cin>>x;
		  for(i=0; i<n; i++)
		  {
		   if(A[i].RBno()==x)
		   {
			  cout<<"Book "<<i+1<<" matches the Book Number you entered. Its details are:\n\n";
			  cout<<"Book Number: "<<A[i].Bno<<"\n";
			  cout<<"Name: "<<A[i].Bname<<"\n";
			  cout<<"Author: "<<A[i].Author<<"\n";
			  cout<<"Price: "<<A[i].Price<<"\n";
			  found1++;
		   }
		  if(!found1)
			cout<<"No book matches the Book Number you entered.";
		  }
		  break;
		}
	case 2:
		{
			cout<<"Enter the name of the book you are looking for:";
			gets(Input);
			for(i=0; i<n; i++)
			{
				if(!(strcmpi(A[i].RBname(),Input)))
				{
					cout<<"Book "<<i+1<<" matches the name you entered. Its details are:\n\n ";
		    cout<<"Book Number: "<<A[i].Bno<<"\n";
				cout<<"Name: "<<A[i].Bname<<"\n";
				cout<<"Author: "<<A[i].Author<<"\n";
				    cout<<"Price: "<<A[i].Price<<"\n";
		    found2++;
				}
			}
				if(!found2)
					cout<<"Sorry. No book matches the name you entered.";
				break;
		}


	}
	getch();
}


But while running it the compiler gives the errors as:
Line 43 to 48: Illegal character '\' (0x5c)
Line 69: Undefined symbol 'Display'
Line 88: 'BOOK::Bno' is not accessible.
Line 89:'BOOK::Bname' is not accessible.
Line 90:'BOOK::Author' is not accesible.
Line 91:'BOOK::Price' is not accesible.
Line 108:'BOOK::Bno' is not accessible.
Line 109:'BOOK::Bname' is not accessible.
Line 110:'BOOK::Author' is not accesible.
Line 111:'BOOK::Price' is not accesible.

from 43 to 48..the line feed was also used at many other places but there it was not given as an error so why here?
Line 69: I defined the Display() function outside the class since it contained control structures, so what's the error then?
About the lines the rest of the error( the "not accessible" ones) I know these data members are not accessible because they are in private visibility mode. But then how to make them accessible? (Without putting them in public because it was a part of the question to create the data members in private). Need help! :)


from 43 to 48..the line feed was also used at many other places but there it was not given as an error so why here?

You have probably got some invalid/invisible characters in your code that your editor doesn't show. This can happen sometimes when copy pasting from other places. Try copy paste the code from this page or retype the code manually.

Line 69: I defined the Display() function outside the class since it contained control structures, so what's the error then?

If you want Display() to be a static member of BOOK then you have to declare it inside the class definition as static void Display(BOOK A[], int N);, and call the function as BOOK::Display(A,n);.

If you don't want Display() to be a member of BOOk you'll have to remove BOOK:: when defining the function and to make the private variables of BOOK accessible from Display() you will have to make Display() a friend of BOOK by placing friend void Display(BOOK A[], int N); inside the class definition.
Last edited on
Topic archived. No new replies allowed.