GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-Oct-2005, 13:43
illbemissingu illbemissingu is offline
New Member
 
Join Date: Oct 2005
Posts: 12
illbemissingu is on a distinguished road
Question

c++ inheritance


Hello everyone,
I'm making some experiment on C++ inheritance and find some confusion.
Say I have 3 classes,
1) class Cart, which has a method
CPP / C++ / C Code:
addGood(Good* aGood)
to add goods to the cart.
2)a Good class, which is inherited by class called Book
3)Book class.
CPP / C++ / C Code:
 Book:: Book();
If I want to add a book object to the cart, I wrote
CPP / C++ / C Code:
 Book book;
         Cart cart;
         cart.add(*book) // this is where I'm confused, the paramter passed

And when I complied it I got an error saying "no match for 'operator*' in '*book' ".
I think we I add an object, I should pass the real object which is the object the variable points to into the cart, instead of just put "book" value as a parameter. But neither way seemed to work.
Could anyone give me any hint?
Thanks!
  #2  
Old 22-Oct-2005, 15:42
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: c++ inheritance


This link has some information,
http://www.parashift.com/c++-faq-lite/
Take a look at 19,20,21,22,23,24,25

If nothing there helps post back with more code.
Because that is not really enough code to see what all you are doing. Are you using public, private or protected with inheritance(example of missing details)?

Usually the smallest complete program that reproduces the error is best.
  #3  
Old 22-Oct-2005, 19:01
illbemissingu illbemissingu is offline
New Member
 
Join Date: Oct 2005
Posts: 12
illbemissingu is on a distinguished road

Re: c++ inheritance


Thanks Sokar, I read the link pages and learned something new. However, problems are still not solved. Like you said, I will make a smallest possible program that demonstrate the error I encountered

This time say there are 4 classes:
1, Cart:
CPP / C++ / C Code:
Cart::Cart() : capacity(defaultCapacity), currentWeight(0), goodies () 
{} // goodies is a private vector holding the Good objects
Cart::~Cart() { }
void Cart::addGood (Good* good) {
     goodies.push_back(*good); // plz check if I did wrong with vector:)
}
2. Good class with de/constructors:
CPP / C++ / C Code:
Good::Good(int weight) : weight(weight) {}
Good::~Good(){ }
3. Book class with de/constructors
CPP / C++ / C Code:
 
Book::Book(int weight, std::string name) : Good(weight), name(name) 
{} // the idea is the constructor has a weight and a name paramter
4. Customer, which I will use to demonstrate the error
CPP / C++ / C Code:
 {Cart mycart;   
          char name[40];   
          // then I read from a input file and get the name string correctly, then see below
          Book mybook(100, *name);// error HERE "invalid conversion from `char' to `const char*' "
     // & another error "initializing argument 1 of `std::basic_string<_CharT, _Traits,  " 
     //(since this is a demonstration, im not sure if the error msg produced here in the original program should be the same for this demonstration or not)
          mycart.addGood(100, *mybook); // error HERE "no match for 'operator*' in '*mybook' "
         }
    

I hope I have demonstrated clearly enough. Sorry for putting such a lot of code, but I think this is the best I can address my problem.
Thanks a lot!!
  #4  
Old 22-Oct-2005, 19:57
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: c++ inheritance


If you have do not have a reason for using char just switch it to,
CPP / C++ / C Code:
std::string name;
and make the call like this,
CPP / C++ / C Code:
Book mybook(100, name);
Your "arguments/parameter list" do not match,
CPP / C++ / C Code:
void Cart::addGood (Good* good)
I see one parameter there. But you try to pass two,
CPP / C++ / C Code:
mycart.addGood(100, *mybook);
maybe this?
CPP / C++ / C Code:
mycart.addGood(*mybook);
  #5  
Old 22-Oct-2005, 20:10
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: c++ inheritance


Hi,

Please take a look at this:

Pass by value and pass by reference:

C++ always passes arguments `by value' by default: a copy of the value of each argument is passed to the function; the function cannot modify the actual argument passed to it:

CPP / C++ / C Code:
void foo(int j)
 {
  j = 0;  /*  modifies the copy of the argument received by the function  */
}

int main(void) {
  int k=10;
  foo(k);
  /*  k still equals 10  */
return 0;
}

If you do want a function to modify its argument you can obtain the desired effect using pointer arguments instead:

CPP / C++ / C Code:
void foo(int *j) {
  *j = 0;               //note how the variable is used here.
}

int main(void) {
  int k=10;
  foo(&k);            //note how variable is passed here.
  /*  k now equals 0  */
return 0;
}
This is also known as `pass by reference'.

Your main program:
CPP / C++ / C Code:
Book mybook(100, *name);// error HERE "invalid conversion from `char' to `const char*' "

mycart.addGood(100, *mybook); // error HERE "no match for 'operator*' in '*mybook' "


So you should use like this:
CPP / C++ / C Code:

Book mybook(100, name);           //because name will give the address in the case of char*.

mycart.addGood(100, &mybook);  //pass the reference


And you may switch to std::string instead of char* as sokar said.

try this:
CPP / C++ / C Code:
using namespace std;
int main()
{
    Cart mycart;
    string name; 
    
    Book mybook(100, &name);           //use &name to pass it by reference. it is different from how you pass a char*
    mycart.addGood(100, &mybook);   //pass the reference of  mybook

    return 0;
}

Note the difference between how a char* is passes and how a string is passed.

Please post your full code at a stretch.(i.e dont split up your code)
That may help more..

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #6  
Old 22-Oct-2005, 20:56
illbemissingu illbemissingu is offline
New Member
 
Join Date: Oct 2005
Posts: 12
illbemissingu is on a distinguished road

Re: c++ inheritance


thanks a lot Sokar!
  #7  
Old 22-Oct-2005, 21:32
illbemissingu illbemissingu is offline
New Member
 
Join Date: Oct 2005
Posts: 12
illbemissingu is on a distinguished road

Re: c++ inheritance


Hi Paramesh,
Thank you for the excellent explanation. That's exactly where I was not clear, and am still but better.
Could you explain in more detail about this example?

CPP / C++ / C Code:
void foo(int *j) 
{
  *j = 0;       // just want to clearify, if im typing foo(j) in main, Im
                  // actually passing the address of j instead of reference,
                  // right? so this would be wrong against what i wanted (the 
                  // object)
}

int main(void) {
  int k=10;
  foo(&k);    //why &k? shouldn't it pass the reference/pointer
                // instead of the address of k?
                // in this case it seems to me that foo is waiting for an 
                // address parameter. I know im wrong, please correct
                // me. thank you!
  /*  k now equals 0  */
return 0;
}
 
  #8  
Old 22-Oct-2005, 22:32
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: c++ inheritance


Hi,

Where are your
CPP / C++ / C Code:
#include "Customer.h"
#include "Rib.h"
#include "Croissant.h"
#include "Jam.h"
#include "Goody.h"

Without knowing that, there is no way to understand your program.
Please post them.

About the errors:
Undefined reference:
When the linker tries to generate the application, it can't find the *definition* of your class or its methods.
Note that definition is different from declaration.

Now to the question you asked:
CPP / C++ / C Code:
  foo(&k);    //why &k? shouldn't it pass the reference/pointer
                // instead of the address of k?
                // in this case it seems to me that foo is waiting for an 
                // address parameter. I know im wrong, please correct
                // me. thank you!

Passing by reference means that we'll pass the addresses of the variables instead of the values.


CPP / C++ / C Code:

  *j = 0;       // just want to clearify, if im typing foo(j) in main, Im
                  // actually passing the address of j instead of reference,
                  // right? so this would be wrong against what i wanted (the 
                  // object)

You need to know more about pointers
You can find more about pointers in these links:
http://www.gidforums.com/t-5393.html
http://www.gidforums.com/t-5640.html

Sorry. I have one more request.
Please tell us what your program should do or what is your assignment.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
  #9  
Old 22-Oct-2005, 23:23
illbemissingu illbemissingu is offline
New Member
 
Join Date: Oct 2005
Posts: 12
illbemissingu is on a distinguished road

Re: c++ inheritance


Hi Paramesh,
Thanks for your explanation and time. The question is to fill a basket of goods until the input file list is empty or the weight in basket excesses the allowed weight, using c++, provided w/ those classes necessary. so what i have to do is maily is to implement the fillBasket method, without touching other classes or headers. It's my first time using c++, so I have quite many questions. still not sure why the linker errors are there.. is it enough to just include the .h header files to make compile?
Thanks again!
  #10  
Old 23-Oct-2005, 00:12
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: c++ inheritance


Hi,

Quote:
is it enough to just include the .h header files to make compile
That depends on the header file. If the header file already contain the definitions for the methods of any class, no need to define it in the main program.
Else you need to define the class functions in your main program.

Quote:
It's my first time using c++, so I have quite many questions
Feel free to ask your doubts.

Quote:
still not sure why the linker errors are there..
Linker takes the object file, combines it with predefined routines from a standard library, and produces an executable program (a set of machine-language instructions)
Why do we get a linker undefined reference error?
When the linker tries to generate the application, it can't find the *definition* of your class or its methods. So you get an error.

Here is a sample code of mine that tells you how to to use header files along with passing arguments as you asked.
header file main.h
CPP / C++ / C Code:
#include<iostream>

class book
{
      public:                                        //public implies that we can use the variables outside the class also
      string book_name;                              //just a variable
      book()                                         //default constructor
      {
            book_name="C++_programming_Language";    //just give a value
      }
};

class good : public book                             //good is the derived class from book
{
      public:
      string good_name;                              //just another string
      good()
      {
            good_name="Book";                        //give it a value in the constructor
      }
};

class cart                                           //class cart
{
      public:
      int items;                                     //number of items
      good* agood;                                   //pointer to your good class
      string cart_name;                              //another string

      cart()
      {
            items=0;                                 //initialize values. items now are zero.
            cart_name="Indian_Airlines";             //give a value to the string.
      }

      void addgood(good* bgood)                      //here is the sample function.
      {
           agood=bgood;                                   //assign your agood member variable to the bgood pointer passed.
           items++;                                          //increase your items by 1
           std::cout<<"One item added to the cart..."<< agood->good_name <<endl;     //this is how you use pointer objects
      }

};


C++ file main.cpp
CPP / C++ / C Code:
#include"main.h"                //include your main.h

#include<iostream>

int main()
{
    good agood;                 //create objects for good and carts.

    cart carts;

    carts.addgood(&agood);      //this is how you pass values.


    return 0;                   //return 0 indicates successful execution
}
    
   
You can find more information in the comments.
Note that i havent included some advanced concepts.
You can learn them as you proceed learning c++.(just forget these two lines)

You can refer the links posted by Sokar to learn more.

Regards,
Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogObservations of Iraq by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
operator overloading Krandygrl00 C++ Forum 5 06-Jul-2005 17:01
Inheritance Project .. Qatar C++ Forum 1 28-Apr-2005 17:04
inheritance problem ap6118 C++ Forum 8 11-Apr-2005 11:24
Inheritance problem CaptnB C++ Forum 2 27-Jan-2005 08:09
Operator overloading & inheritance Imajica C++ Forum 1 15-Apr-2004 20:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 11:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.