Object Oriented Problem
Hi there. i have been learning programming for about 4 years and have quite an understanding of modular and procedural programming (using vb and pascal) in the last 8 months ive been buying book upon book trying to learn about C++ and the object oriented approach, reading them just leaves me more confused. its the same deal with tutorials online. learning the c++ isnt a problem for me it the oop that i just cannot get my head around. is anyone willing to help me out a little? i should really say that programming is a part time hobby of mine whilst still going to an I.T college.
[584 byte] By [
roooss] at [2007-11-11 8:28:16]

# 1 Re: Object Oriented Problem
If you need definitive guidance, there is no better book than The C++ Programming Language (just as nothing beats Kernighan & Ritchie for C).
From a C++ perspective it's also worth keeping in mind what C++'s particular strengths in this area are. For example:
- compile-time generic classes using templates (the std containers are good examples, but templates are immensely useful for many applications)
- stack-based resource acquisition (letting data members fall out of scope is in many ways cleaner and easier than depending on garbage collectors); if you make sure all 'new' allocations are done within smart pointers (e.g. std::auto_ptr and boost::shared_ptr), you never need to call delete again
- writing functional wrappers for well-established C libraries is trivial
Bytecode languages have got their advantages, of course, but as C++ users I think we should focus on the strengths of the language (and avoid the memory pitfalls that are often cited by fans of Java, C#, and so on).
# 4 Re: Object Oriented Problem
Here's a pared down example of the things I mentioned in my earlier post.
The program reads an XML file specified on the command line and writes it as UTF-8 to standard output.
First we have a very basic wrapper class around a C library, Expat.
#ifndef WRAP_EXPAT_H
#define WRAP_EXPAT_H
#include <expat.h>
#include <string>
#include <vector>
class WrapExpat
{
public:
WrapExpat(bool nameSpaceAware = false);
virtual ~WrapExpat();
bool parse(const string& buffer);
std::string getLastError();
protected:
XML_Parser p;
};
#endif
Next we have a class derived from WrapExpat, XmlUtf8Reader, that stores the XML in a std::string object.
#ifndef XML_UTF_READER_H
#define XML_UTF_READER_H
#include <string>
#include "wrapexpat.h"
class XmlUtf8Reader : public WrapExpat
{
public:
XmlUtf8Reader(bool parseDeclaration = true, size_t size = BUFSIZ);
virtual ~XmlUtf8Reader();
std::string getBuffer();
private:
std::string buffer;
static void XMLCALL xmldeclhandler(
void *data,
const XML_Char *version,
const XML_Char *encoding,
int standalone);
static void XMLCALL defaulthandler(
void *data,
const XML_Char *s,
int len);
};
#endif
With these two classes in place, we can write the following miniature application:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <memory>
#include "xmlutf8reader.h"
int main(int argc, char *argv[])
{
if (argc < 2)
{
std::cerr << "Usage: myprog <input file>" << std::endl;
return 1;
}
std::ifstream ifs(*(++argv));
if (!ifs)
{
return 2;
}
std::stringstream iss;
iss << ifs.rdbuf();
std::string buffer(iss.str());
std::auto_ptr<XmlUtf8Reader> reader(new XmlUtf8Reader());
if (!reader->parse(buffer))
{
std::cerr << reader->getLastError();
return 3;
}
std::cout << reader->getBuffer();
return 0;
}
Is this the kind of thing you have in mind?
# 6 Re: Object Oriented Problem
If you're looking for step by step instructions, a tutorial would be a good place to start. The Wikipedia article on C++ (http://en.wikipedia.org/wiki/C_Plus_Plus) lists some of them.
What I would say, though, is that it's much more fun to start with a problem you want to solve. Perhaps a program that reads a *.wav file and saves a copy that runs twice as fast? (That's the example I meant to post but I don't have access to the files at the moment.) There are so many useful things one can write there really is no need to create a class Person, then a derived class Employee, then FullTimeEmployee and PartTimeEmployee, etc.