help needed Updating the values of an object in class

I am making a program on classes and i am unable to find a way to upda

Page 1 of 1

13 Replies - 697 Views - Last Post: 30 April 2009 - 10:03 AM Rate Topic: -----

#1 coolnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 09

help needed Updating the values of an object in class

Post icon  Posted 27 April 2009 - 10:23 AM

My Question :-

I have to create a class with name library and have to accept the following values as the class object :-
1. Book Name
2. Book number
3. Total no. of that particular book available (copies)
4. Author of that book
5. price of that book

And i have to make the following functions :-
1. To accept the values of 50 Books i.e. their individual name, number, copies, auther and price.
2. To output the the individual values of book.
3. To find a book and issue it to a person.
4. To update the value of book when it is issued and returned.

Where i am stuck...
> I have made and class with the name provided as well as using arrays i have assigned the individual vaues of 50 books.
And I'm also able to output the value as well as i have also done the issuing part.

[/u]In short... i am stuck at my 4th step.

If you want me to write the full code of the program, ill do that as well.

Hi, i am a noob in this forum will need all your support and help to become one of the best programmer. And if i break any rules do let me know, so that i will be careful in future.

Thanks Nish

Is This A Good Question/Topic? 0
  • +

Replies To: help needed Updating the values of an object in class

#2 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,781
  • Joined: 20-September 08

Re: help needed Updating the values of an object in class

Posted 27 April 2009 - 10:56 AM

This sounds like an interesting problem ... but ...

Do you need to use an array ... Why not a C++ STL vector (or list) of struct ( or class) to hold the data on each book ?

And do multiple copies of the same book get different ID numbers each ... or share the same call number but have a different copy number ?

You need to be exact about what spec's you are to meet.

And of course ... you need to show what code you have so far ...

You might like to see this ...

http://www.dreaminco...snippet2614.htm

This post has been edited by David W: 27 April 2009 - 10:59 AM

Was This Post Helpful? 0
  • +
  • -

#3 coolnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 09

Re: help needed Updating the values of an object in class

Posted 27 April 2009 - 11:22 AM

Ill edit my code and will write the program. But as you have mentioned something about vectors etc... the thing is that i am just restricted to constructor, classes, functions etc. and not advance programing.

By the way thanks for that link.
Was This Post Helpful? 0
  • +
  • -

#4 David W  Icon User is offline

  • DIC supporter
  • member icon

Reputation: 277
  • View blog
  • Posts: 1,781
  • Joined: 20-September 08

Re: help needed Updating the values of an object in class

Posted 27 April 2009 - 11:50 AM

View Postcoolnick, on 27 Apr, 2009 - 10:22 AM, said:

Ill edit my code and will write the program. But as you have mentioned something about vectors etc... the thing is that i am just restricted to constructor, classes, functions etc. and not advance programing.

By the way thanks for that link.


When you are using 'classes' you are ready to use the STL class vector ...

But you could use an array of your objects if you wish ... instead of a vector or a list or ... to hold your objects.
Was This Post Helpful? 0
  • +
  • -

#5 coolnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 10:45 AM

Ok.... now here is the class i have created... I wanted to know whether the following class will work for my code or not.

class library
{ 

public:
int bookno[50];
int no_of_copies[50];
char bookname[50];
char author[50];
int price[5];


void getdata()
{ 

cin>>bookno;
cin>>no_of_copies;
cin>>bookname;
cin>>author;
cin>>price;
}

void update()
{

char bookname;
cin>>bookname;
for(int i=0;i<50;i++)
{
 if(strcmp(bookname,obj[i]bookname)==0)
  {
	obj[i].getdata;
   }
}

 void display()
{
cout<<bookname<<endl;
cout<<no_of_copies<<endl;
cout<<book_no<<endl;
cout<<author<<endl;
cout<<price<<endl;
}

}


Kindly help me with this class

Was This Post Helpful? 0
  • +
  • -

#6 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 11:03 AM

Well, I don't think it will compile
I'm just wondering (first time with classes?)

Some code fixes below, see individual comments
class library
{

//public:  //don't make data that the user doesn't need access to 
//			 public data, keep it private (the whole point of classes)
			   //the member functions that you write can access and 
			   //and change the data, allow the user to change
			   //data through the functions.
private:
int bookno[50];
int no_of_copies[50];
char bookname[50];
char author[50];
int price[5];

//functions should be public, so user can change the data
public:

//for all functions, you will need to tell the compiler that these are
//are part of it (this is because normally we declare a header file, 
// and an implementation file where the functions
// are actually written in the implementation, and just mentioned in // the header
//either way
void library::getdata()
{

cin>>bookno;
cin>>no_of_copies;
cin>>bookname;
cin>>author;
cin>>price;
}

void library::update()
{

char bookname;
cin>>bookname;
for(int i=0;i<50;i++)
{
 if(strcmp(bookname,obj[i]bookname)==0)
  {
	obj[i].getdata;
   }
}

 void library::display()
{
cout<<bookname<<endl;
cout<<no_of_copies<<endl;
cout<<book_no<<endl;
cout<<author<<endl;
cout<<price<<endl;
}

};  //you forgot the ';' on this parenthesis.  VERY IMPORTANT


Was This Post Helpful? 0
  • +
  • -

#7 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 11:38 AM

Hi, cool nick
I noticed you had this in your description

Quote

I have to create a class with name library and have to accept the following values as the class object :-
1. Book Name
2. Book number
3. Total no. of that particular book available (copies)
4. Author of that book
5. price of that book


So, on that note, let me show you what you should be doing

Allow me to explain, first I'm assuming you know what a "function prototype" is. If not just google that or I can elaborate more

In the code below I have altered your private data, for a more object oriented point of view.
What you should take from this, is essentially since you need a library of books (say 50 different books), we are going to create 1 class that will hold just 1 book value. Include it in the main file, and use the class 50 times (I'll probably use an array and each element will hold a class)

Your going to put your data, and function prototypes (including constructor and destructor) in the header file like this

Library.h (header file)
//for a class header file (this file)
//you will want to have something called "file guards"
//at this point you should just need to know how to use them

//file guards
#ifndef LIBRARY_H
#define LIBRARY_H

//now include the other headers you will need
//i have declared strings as some private data
#include <iostream>
#include <string>

using namespace std;

class library
{

//public:  //don't make data that the user doesn't need access to
//			 public data, keep it private (the whole point of classes)
			   //the member functions that you write can access and
			   //and change the data, allow the user to change
			   //data through the functions.
private:
string name;  //this is the title
int num;  //book number
int copies;  //amount of copies available.
string author;  //author of the book
double price;

//functions should be public, so user can change the data
public:
library();  //constructor
~library();  //destructor
void getdata();  //get the initial data
void display();  //another func
void update();  

};  //you forgot the ';' on this parenthesis.  VERY IMPORTANT

#endif  //the end file guard is needed



And the code for the actual functions should be stored in something called the implementation file.
I'm not going to write the whole file for you, but what I show you should allow you to finish the file.
Also please look in the tutorial section of c++ to find a tutorial on classes.

Library.cpp (implementation file)
//first include your header file, and it will bring with it the includes you have already used
#include "Library.h"

//now just program the actual code into the functions you declared in the header file
//for example
void library::getdata()
{

cin>>bookno;
cin>>no_of_copies;
cin>>bookname;
cin>>author;
cin>>price;
}

// you will need to do this for all of your functions
void library::update()
{

char bookname;
cin>>bookname;
for(int i=0;i<50;i++)
{
 if(strcmp(bookname,obj[i]bookname)==0)
  {
	obj[i].getdata;
   }
}


//nothing left in this file.



To access the functions in your main .cpp file
you will use the dot operator (.) and then type the function name and parenthesis

This post has been edited by Notorion: 28 April 2009 - 11:50 AM

Was This Post Helpful? 0
  • +
  • -

#8 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 12:04 PM

@ Notorion:
I know this does not concern the OP's question, but I'm picking up C++ and have a question. With regards to the header file, is that roughly equivalent to an interface in Java? Do all class that you create need to have a header file?

My apologies to coolnick for using his post, but I figured Notorion would be watching this post and this information may help you out in the long run (if you're familiar with Java)
Was This Post Helpful? 0
  • +
  • -

#9 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 12:36 PM

Sorry for the delay, I was in a meeting.

I'm checking on the differences specifically with class headers between java and c++ at the moment.

As far as your question, the class doesn't "need" to have a header file per say; However it is done in standard practice that a header file and an implementation file is used with classes. The purpose of this is to make the class much more clear, in exactly what is defined, what the class is doing in addition to making it easier to change how the class handles the data internally in the implementation file.
Was This Post Helpful? 0
  • +
  • -

#10 Notorion  Icon User is offline

  • D.I.C Regular

Reputation: 35
  • View blog
  • Posts: 378
  • Joined: 17-February 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 12:56 PM

As to your Java question, I have a website you can look at that was presented as an introduction to Java, considering I know c++

http://www.cs.uky.ed...9/CS216Java.htm

Important Information:

Quote

Comparison with C++

No pointers (Addresses). Objects and arrays are passed by reference, everything else is passed by value.

No null terminated character strings, but a String class

No C++ runtime functions (i/o, numeric conversions), but equivalent classes

No global variables, everything must be in a class

No preprocessor (no “#” statements, #include, #if, etc.)

No structures or unions

No typedefs

No goto

No separation of class definition (.h file) and implementation (.cpp file). The class is defined and implemented in the same file (.java source code, .class executable byte codes)

Everything is defined in terms of classes and objects of classes (no exceptions as with C++). No functions outside of classes

But conditional statements (for, while, if, switch) are the same, operators (+,-,++,==etc.) are the same.

Java has try/catch/throw exception handling (with an added keyword “finally” as a “catchall” catch block after other catch blocks may be executed).

Java has no need for prototype definitions before you use a function.

Java has a “const” for constant terms (but calls it “final”).

And there are other differences you will learn when writing a real Java program


What we are really worried about with this to your specific questions is

Quote

No separation of class definition (.h file) and implementation (.cpp file). The class is defined and implemented in the same file (.java source code, .class executable byte codes)


this means, for C++ that the files ARE separate, as I was showing above, but in Java you wouldn't deal with prototypes of the functions (Methods I think they are called in Java).
Rather, in Java you just make the class, name the Method and the parameters, and then write the code right there in the Method. In C++ you would split them up as shown above.

This post has been edited by Notorion: 28 April 2009 - 12:57 PM

Was This Post Helpful? 1
  • +
  • -

#11 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 02:26 PM

C++ has header files so that other classes can know about a class, which doesn't happen automatically. If you want classes to interact with each other, they need to have header files in order to understand each other and communicate. Java has interfaces to allow for inheritance and polymorphic behavior. They have a similar idea, being a class without method implementations, but aren't really the same and serve different purposes. (I posted a question about the exact same thing in the java forum a while ago :D ).
Was This Post Helpful? 1
  • +
  • -

#12 Ed_Bighead  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 14
  • View blog
  • Posts: 178
  • Joined: 26-April 09

Re: help needed Updating the values of an object in class

Posted 28 April 2009 - 08:37 PM

@Notorion and crazyjugglerdrummer

Thanks for the info. I knew about using interfaces with regards to inheritance and polymorphism in Java, but had never used classes in C++. This cleared things up.
Was This Post Helpful? 0
  • +
  • -

#13 coolnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 09

Re: help needed Updating the values of an object in class

Posted 29 April 2009 - 12:25 PM

Hi guys, I am glad that this forum is very active and not like other forum where people take so much time to reply to the topic.

I have read all the above replies and have decided to once again recreate my
program. I will post my full and final coding tomorrow in the eveing IST and
this time i bet that its going to be a far better coding that i have done before
and all the credit goes to the active member of this forum.

Thanks guys, and make sure you read my final stuff tomorrow.
Reg,
Nish
Was This Post Helpful? 0
  • +
  • -

#14 coolnick  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 27-April 09

Re: help needed Updating the values of an object in class

Posted 30 April 2009 - 10:03 AM

Hi guys,
I am rewriting the code and this time with a little different values...
please have a look at it and tell me will the following will work or not.

/* This is the program where i am just taking in the value
 for 5 books and am outputing the value...ill make a function
 for updating the value of the data. */
class library
{ 
private:

int book_no;
char bookname[30];
int bookissue;
int  bookavail;
int bookreturn;

public :

void getdata()
{
cin>>book_no;
cin>>bookname;
cin>>bookissue;
cin>>bookavail;
cin>>bookreturn;
}

void displaydata()
{
cout<<book.no<<"\t";
cout<<bookname<<"\t";
cout<<bookissue<<"\t";
cout<<bookavail<<"\t";
cout<<bookreturn<<"\t";
}
}detail[5];

void main
{ clrscr();
cout<<"Welcome to book lib v1.0;
cout<<endl;
cout<<"would you like to enter details for the book"<<endl;
char t;
cin>>t;

// I was thinking of making this part as function... What say ??

if(t=='y')||(t=='Y')
{
for (int i=0;i<5;i++)
{cout<<"enter the value for "i+1 "book:"<<endl;
detail[i].getdata();cout<<endl;
}
else
cout<<"now would you like to see the details of your book";
cout<<endl<<"If yes then enter Y again else enter n";
cout<<endl;
char f;
cin>>f;
if(f=='y')||(f=='Y')
{ for(int j=0;j<5;j++)
{ cout<<"displaying the details for " j+1" book";
cout<<endl;
detail[i].displaydata();
cout<endl;
}
}
else(f=='n')||(f=='N')
{ cout<<"So, would you like to exit the program..."<<endl;
   cout<<"enter Y to exit the program"<<endl;
char Q;
cin>>Q;
if(Q==='y')||(Q=='Y')
{exit(1);
}

// I am writing the other half....

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1