help needed Updating the values of an object in classI am making a program on classes and i am unable to find a way to upda
Page 1 of 1
13 Replies - 567 Views - Last Post: 30 April 2009 - 10:03 AM
#1
help needed Updating the values of an object in class
Posted 27 April 2009 - 10:23 AM
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
Replies To: help needed Updating the values of an object in class
#2
Re: help needed Updating the values of an object in class
Posted 27 April 2009 - 10:56 AM
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
#3
Re: help needed Updating the values of an object in class
Posted 27 April 2009 - 11:22 AM
By the way thanks for that link.
#4
Re: help needed Updating the values of an object in class
Posted 27 April 2009 - 11:50 AM
coolnick, on 27 Apr, 2009 - 10:22 AM, said:
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.
#5
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 10:45 AM
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
#6
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 11:03 AM
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
#7
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 11:38 AM
I noticed you had this in your description
Quote
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
#8
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 12:04 PM
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)
#9
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 12:36 PM
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.
#10
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 12:56 PM
http://www.cs.uky.ed...9/CS216Java.htm
Important Information:
Quote
· 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
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
#11
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 02:26 PM
#12
Re: help needed Updating the values of an object in class
Posted 28 April 2009 - 08:37 PM
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.
#13
Re: help needed Updating the values of an object in class
Posted 29 April 2009 - 12:25 PM
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
#14
Re: help needed Updating the values of an object in class
Posted 30 April 2009 - 10:03 AM
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....
|
|

New Topic/Question
Reply




MultiQuote





|