Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

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).
geralds at 2007-11-11 21:01:20 >
# 2 Re: Object Oriented Problem
Start simple. A record in pascal is a lot like a class. But a class has the functions that work on the class'es data fields. This is where to start -- a simple class with a few functions that is much like your modular programming. Work that up and slowly add things -- overload an operator, add a static variable, inherit (derive) a class from another. The more complex OOP stuff can be shelved for quite some time until the simpler stuff makes sense. Study classes before templates -- all the way thru the harder OOP concepts -- templates really require a good OOP knowledge.
jonnin at 2007-11-11 21:02:25 >
# 3 Re: Object Oriented Problem
thank you for your responses. i understand that a class is a data type of an object that also holds the functions for that class. i also understand the idea of child classes inheriting state and behaviour infromation (although i am unsure in how to determine these parameters for myself.). i have now begun to understand encapsulation and the hiding of imformation (but get lost with multiple inheritance and typedef). i really need help with how to put this all together with c++ in a situation. has anybody got a few simple example programs where the object oriented approach has been used? i figure if i see how its applied i can work with that.
roooss at 2007-11-11 21:03:20 >
# 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?
geralds at 2007-11-11 21:04:19 >
# 5 Re: Object Oriented Problem
i can make some sense out of all of this code i can see where you have created your classes though that is the limit, im affraid i have never delved into anything about xml so i have no idea of the function of the program is or any of the code inside the program. i think i can get teh general gist of what is going on.. however i have gone to compile it in devc++ and it says that expat.h doesnt exist. i am very greatful for the input i have learned from it! thank you geralds. would anybody be willing to spend a short time with me at some point and walk me through creating a very small object oriented program? maybe as im going through it with someone there on hand i can get a great deal out of it. im thoroughly determined to learn c++ (i got plenty of time im only 18!) and im determined to figure out this oop subject.
roooss at 2007-11-11 21:05:29 >
# 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.
geralds at 2007-11-11 21:06:24 >