Initializing const data member

Here's a part of my program. What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category (with cascading capacity if possible. I also posted some of my set functions for further comprehension.



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
class Book
{
	
	friend void CompPrice(Book &,Book&);                         //friend function that has access to the member functions of this class
	                                                             //The arguments sent to it are by address, and of type the class Book, so that it can have access to its member functions
private:                                                         //private data members
	
	char *BookName;                                              //creating a pointer to use for the dynamic array
	const char Category;                                         //const data member
	int Pages;                                                   
	int Pub_Date;
	double Price;
	Shelfmark Var;                                               //Shelfmark data member in class Book (composition)
	static double VAT;                                           //static data member VAT cant be initialized inside the constructor nor inside the class

	

public:
	Book():Category('S')                                         //Default constructor
	{                                                            //Category defaulted in same line as definition because its a const data member
		BookName=NULL;                                           //BookName is an empty string
	    Pages=20;                                                //Default values
		Pub_Date=1998;
	    Price=2;
		Var.setNum2(0);                                          //set  functions used to access the private data members of class Shelfmark to initialise them to 0
		Var.setNum1(0);
		
	}

	Book(char *Array,int N,int P,double K, int D,int C, int O, int Q):Category((C=='R' || C=='A' || C=='S')?C:'S')
		//Overloaded constructor that takes arguments. Category's conditions are in the same line as the consturctor definition
	{
		setBookName(Array,N);                                    //setBookName function takes a pointer as an arguemnt along with an integer
		setPages(P);                                             //setPages function takes an int as an argument
		setPub_Date(D);                                          //setPub_Date function that takes an int as an argument
		setPrice(K);                                             //setPrice function that takes a double value as an argument
		setVar1(O);                                              //setVar1 function takes int as an argument
		setVar2(Q);                                              //setVar2 function takes int as an argument
	}

	Book &setBookName (char *Array, int N)                       //return type is Book & (used for cascading capacity)
	{
		{
		if (Array)                                               //If an array is passed, enter the if scope
			{
				BookName= new char[strlen(Array)+1];             //Creating an array of har dynamically
				assert(BookName!=0);                             //using assert to make sure we have enough space in memory to create the new array
	strcpy(BookName,Array);                       //copying the char in Array to Bookname
	BookName[strlen(Array)]='\0';                              //placing the null terminator in the end
		}
		else BookName=NULL;                                      //If an array isnt passed, Bookname will point to an empty string
	}
		return *this;                                            //returning *this for Cascading capacity
	
	}

	Book & setPages (int P)                                      //return type is Book & for cascading capacity
	{
	Pages=(P>0?P:20);                                            //Checking for P's validity using the ?: terminator
	return *this;                                                //return *this for cascading capacity. (*this pointer to the variable that called the function)
	}


Thanks for your assistance
Regards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class a_class
{
  private:
    const int idm;
  public:
    a_class() : idm(0){}
    a_class(int ival) : idm(ival){}
};

int main()
{
  a_class obj1;
  a_class obj2(1);
  return 0;
}

What I need to know is how I can pass an argument to the Book constructor so I can change the const data member Category
? What are you doing on line 30?

By the way: the whole idea of const is that you cannot change it. You can only initialize it
Topic archived. No new replies allowed.