ABC Problem

This is a discussion on ABC Problem within the C++ Programming forums, part of the General Programming Boards category; My ABC is: Code: //holding.h #ifndef _HOLDING_H #define _HOLDING_H #include <stdlib.h> #include <iostream> #include <fstream> using namespace std; class Holding{ ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    92

    ABC Problem

    My ABC is:
    Code:
    //holding.h
    
    #ifndef _HOLDING_H
    #define _HOLDING_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    class Holding{
          
          protected:
                    
                    char *title;
                    int titleLength;
                    int callNumber;
                  
          public:
                 
                 Holding(char *, int);
                 Holding(const Holding &);
                 virtual ~Holding();
                 
                 virtual void print(ostream &) = 0;
                 
          };
    
    #endif
    
    // holding.cpp
    
    #include "holding.h"
    
    Holding::Holding(char *tempTitle, int tempCallNumber){
                          
                          titleLength = strlen(tempTitle);
                          title = new char[titleLength + 1];
                          strcpy(title, tempTitle);
                          title[titleLength] = '\0';
                          
                          callNumber = tempCallNumber;
                          
                          }
    Holding::Holding(const Holding &temp){
                           
                           titleLength = temp.titleLength;
                           title = new char[titleLength + 1];
                           strcpy(title, temp.title);
                           title[titleLength] = '\0';
                           
                           callNumber = temp.callNumber;
                           
                           }
    Holding::~Holding(){
            
            delete [] title;
            
            }
    My derived class is:
    Code:
    // Book.h
    
    #ifndef _BOOK_H
    #define _BOOK_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    #include "holding.h"
    
    class Book : Holding {
          
          protected:
                    
                    char * author;
                    int authorLength;
                    
          public:
                 
                 Book(char *, char *, int);
                 Book(const Book &);
                 virtual ~Book();
                 
                 virtual void print(ostream &);
                 
          };
    
    #endif
    
    // Book.cpp
    
    #include "book.h"
    
    Book::Book(char *tempAuthor, char * title, int callNumber) : Holding(title, callNumber) {
                    
                    authorLength = strlen(tempAuthor);
                    author = new char[authorLength + 1];
                    strcpy(author, tempAuthor);
                    author[authorLength] = '\0';
                    
                    }
    Book::Book(const Book &temp) : Holding(temp) {
                     
                     authorLength = temp.authorLength;
                     author = new char[authorLength + 1];
                     strcpy(author, temp.author);
                     author[authorLength] = '\0';
                     
                     }
    Book::~Book(){
            
            delete [] author;
            
            }
    void Book::print(ostream &out){
            
            cout << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
            out << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
            
            }
    My main is:

    Code:
    // library.h
    
    #ifndef _LIBRARY_H
    #define _LIBRARY_H
    
    #include <stdlib.h>
    #include <iostream>
    #include <fstream>
    
    #include "holding.h"
    #include "holding.cpp"
    
    #include "book.h"
    #include "book.cpp"
    
    #endif
    
    // library.cpp
    
    #include <iostream>
    #include <fstream>
    #include <stdlib.h>
    
    #include "library.h"
    
    ofstream csis;
    
    int main(){
        
        csis.open("csis.dat");
        
        char type;
        char format;
        char callNumber[100];
        char author[100];
        char title[100];
        
        Book *book;
        Holding *Holdings[5];
        book = new Book("Jeremiah", "Loo", 125);
        Holdings[0] = book;
        }
    In the end I'm getting this error:
    Code:
    'Holding' is an inaccessible base of 'Book'
    I don't know why it's having trouble access it. I believe I clearly derived Book from Holding. Thanks for any suggestions or help.
    Last edited by warfang; 05-07-2007 at 01:28 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,445
    You are using private inheritance. It should be:
    Code:
    class Book : public Holding
    Incidentally, do not use names that begin with an underscore followed by an uppercase character. Those are reserved to the implementation for any use.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Thanks. I've been looking at that in my book and online but I'm so used to seeing class declarations that the extra "public" escaped my attention.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    By the way:

    #ifndef _HOLDING_H

    Bad. Only the compiler is allowed to use identifiers starting with an underscore followed by an uppercase letter for its own private purposes.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Quote Originally Posted by CornedBee View Post
    By the way:

    #ifndef _HOLDING_H

    Bad. Only the compiler is allowed to use identifiers starting with an underscore followed by an uppercase letter for its own private purposes.
    No, not true. They are needed and are very important for a programmer to use.

    http://www.fredosaurus.com/notes-cpp...sor/ifdef.html

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    > No, not true.
    The bit about leading underscores is.

    #ifndef HOLDING_H
    would have been perfectly acceptable.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Mar 2007
    Posts
    92
    Ah, good info to know.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    To be precise, identifiers beginning with an underscore, followed by another underscore or an uppercase letter, are reserved by the implementation. So _variable is okay, but __variable and _Variable are not.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    If you want to be even more precise, identifiers that contain a double underscore anywhere in the name are reserved. So vari_able is ok, but vari__able is not.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I didn't know that. You learn something new every day.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    And if you want to be yet more precise, identifiers starting with a single underscore followed by a lowercase letter or digit are reserved in the global namespace, i.e. you must not define a macro or global identifier (variable, function, type) with such a name.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  2. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 08:38 AM
  3. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  4. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  5. interesting problem
    By kris.c in forum C Programming
    Replies: 17
    Last Post: 08-26-2006, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21