A step by step tutorial teaching you how to create your own chat client and chat server easily in C#, for local networks or the Internet.
A C# tutorial showing you how to make use of WMI to extract information on disk drives, such as model, capacity, sectors and serial number.
This tutorial will teach you how to calculate the shipping cost based on the weight, height, length and depth of the box, the distance and the UPS service type.
Creating a Rich Text Editor using JavaScript is easier to do than you might think, thanks to the support of modern browsers; this tutorial will walk you through it.
Introduction to Object Oriented ProgrammingFor those wishing to delve into the world of OOP using C++. You should have a little bit of programming experience to get the most from this tutorial. |
On Sunday, April 24th 2005 at 12:40 AM By Nick Fletcher (View Profile) ![]() ![]() ![]() ![]() (Rated 4.5 with 14 votes) |
||
--OOP (Object Orientated Programming/Paradigm)--I've heard a couple of people mention that they'd like to see a C++ tutorial that explained more about the OOP side of the language. After all, you can do nearly everything in C that you can do with C++ if you don't hit on the OOP areas. C has no bool type amongst the non OOP dis-simarlarities. I recently put out a tutorial on how OOP linked lists work and I was thinking that people with no experience in OOP would not get much value from that tutorial. So I am writing this one! I won't cover every detail and built in functionality of the C++ language as there are really good books for that. This is simply an intro to the concepts and syntax, and hopefully it gets you thinking about the possibilities of the language. So, who should read this tutorial? Well, it's not an intro the C language. It's an intro to C++ and the associated syntax as mentioned. There are good tutorials on this website that will bring you up to speed on C. What I am trying to say is that C++ is built using most of the syntax of C, with various other language artifacts thrown in on top of C. This tutorial picks up from where C ends really. You'll get the picture... Just read on! A Class of ObjectsIt's hard to come up with original analogies. Animals are good to use for C++, as are Cars and Televisions, but I'll use something that I don't think has been used before...The Computer. That's correct. I think it's the best example of an OOP analogy that I can think of, in a kind of ironic way. You know we are working with objects, because that's part of the OOP acronym. And, you have heard the term class before, I am guessing. So let's begin the explanation of C++ OOP... In a procedural language like C, you have to describe problems with the method of solving the problem, firmly in your mind. The method of solving the problem is with a computer program and computers like their programs to be nice and relational to themselves. It's sometimes hard to juggle your problem solving skills and play nice with the computer at the same time. You can get away with not knowing much about the hardware, but there comes a time when you need to treat some programs in a special way that either the Operating System or the compiler demand. This is not good. You should not have to worry about these things. You should only have to worry about the problem you are attempting to solve, that way you will solve the problem better! We can do this with classes. Sure, you say, but I prefer to read tutorials rather than go to school.. Not those type of classes! I'm talking about the ones where you can give the computer a blueprint for the production of your new tool. It's a super tool, it can do anything you like, and once you have built one, you can organise a whole toolbox full of them. The thing with OOP is that you are still using machine instructions to tell the CPU and some peripherals what to do. You always will have to do it this way! The compiler helps you tell the CPU which machine instructions you want to use, but in the end, the CPU does not understand OOP, C++, APL or how to play an MP3! It's time consuming and error prone to organise a list of machine instructions, place them in memory and execute them... So of course, we invented high level languages(HLL). We get the compiler to sort out our source code into machine code which is nothing more than a pattern of bits that tickle the CPU in certain ways. The CPU greedily eats up these machine codes according to which one his/her instruction pointer falls on. But enough of machine code. That's not what I want to talk about. I want to explain OOP and C++. So, let's get started. Nick? Show Us Some Code...We are surrounded by object orientated design! Really. Take just about anything you use in your day to day life and think of what it would take to manufacture that item yourself. Hmm.. Let's see... A television? It has a brand name on it, but do you think that the company that makes the television manufactured every peice of that T.V? No way! They'd never get anything out the door if that was the case, or the factory would be so large, they'd need a mono-rail system to get from cathode-ray tubes to buttons... No. They out-source production requirements to other specialist companies who in turn, probably out-source even further. What we end up with is television. Now... Do you think that when ACME TV's build a new model, they start with a clean blackboard? No. When they improve upon an older model, do they have to burn the old plans and re-design from the ground up? Nah. An object is a module of combined code and procedures. That code ends up being machine code, the garden variety. It's not magic. You have to make functions and you have to make varibles when you work in C++. There is a lot of work, as much as in any non-oop language and it's still hard to design programs and make them work properly. But.. They said that OOP was the way of the future! They said that all of our programming problems were solved! Were they lying? No. Most of what they said was true! The functions and varibles and problems I just spoke of are the ingredients for C++. We bake up some nice objects with C++ and it's those objects that provide the benefits that they spoke of. In the intro, I mentioned that I would use a Computer as my OOP analogy. A computer is a good example, I think, because it is such a component based entity. Many of the components that combine to provide us with a running computer system can be desribed in C++ terms. I'm going to start describing bits of the computer as objects, and then I'll provide some code to support my claims. First up, the keyboard: Things a keyboard can do:
Things that describe a keyboard:
Now, in C++ code:
Instantiating and accessingWhat we have seen is a class, fully declared and defined. A class does not do much for us. A class is the blueprint for an object. A software object, but nonetheless, an object. Let's make something with that Keyboard blue print.
Dissection of First OOPThere's nothing in that one that you haven't seen before. Well, apart from the using namespace std and #include> iostream <> stuff. The namespace stuff is not going to be handled in this tutorial. But in a nutshell, you can declare specific boundaries where names can exist and if you tell the compiler that you are working within the 'scope' of that namespace, any identical names you work with on the outside of that namespace will not clash. Namespaces are declared almost exactly like classes and you just give a list of all the names you want to scope to that namespace.
Construction and DestructionThere was a part of the Keyboard declaration thay I hoped you would not notice. It's that part that looks like this
Those are also functions of the Keyboard class. The first one is known as a constructor and the second is known as a destructor. Yes.... The destructor is a very evil function. Just kidding... The job of the constructor is really quite simple and goes along with our mentality of what OOP is all about. It initialises the variables of objects when they are first instantiated. Well, it can do more than that, but at this early stage, think of the constructor as... Um... The constructor??? Yes, that's it! It's a really accurate name for this function. The object is constructed. And it gets destructed also. This happens usually when the program ends and the object goes out of scope. But forget about the destructor for now. Let's see the constructor in action! #include using namespace std; class Keyboard { public: Keyboard(int); ~Keyboard(){} void SendScanCode(int theCode); void LaunchProgram(char* theProgramName); void VolumeAdjust(int amount); void SetLastScanCode(int sc); int GetLastScanCodeSent(){ return LSCS; } private: char Color[10]; char Layout[15]; bool HasSpecialKeys; bool IsWireless; int LSCS; }; int main(void) { Keyboard qwerty(12); cout << "Here is the newly built object's LSCS value: " << qwerty.GetLastScanCodeSent() << endl; qwerty.SendScanCode(66); cout << "Here is the last scan code sent: " << qwerty.GetLastScanCodeSent() << endl; getchar(); return 0; } //constructor definition follows... Keyboard::Keyboard(int initialLSCS) { LSCS = initialLSCS; } void Keyboard::SendScanCode(int theCode) { //MrScreen = DoCodeSendingRoutine(theCode); LSCS = theCode; } void Keyboard::LaunchProgram(char* theProgramName) { //MrOsPleaseLaunchThisProgram(theProgramName); } void Keyboard::VolumeAdjust(int amount) { /*if(amount > 0) { volume += amount; } else volume -= amount;*/ } void Keyboard::SetLastScanCode(int sc) { LSCS = sc; } We have in effect, 'passed along' some special startup info we would like for the object. Look carefully at how the data is given to the object. Play around with the constructor. Try adding a string instead of an int, see what complaint the compiler comes up with! That's all for this super thin intro to C++, and I hope you enjoyed it, but I'd like to leave you with some challenging exercises to cement the stuff you have learnt.
Please remember that this is a Primer, designed to be perused before the OOP Linked List part II tutorial. If enough people want me to, I'll present another more advanced tutorial dealing with the things that make C++ really powerful. What I have given you here though is the basic syntax. Play around and try things out. Then you will be ready for the stuff that makes C++ a true OOP language. |
|||
Digg It!
Del.icio.us
Reddit
StumbleIt
Newsvine
Furl
BlinkList
|
|||
|
|||
Current CommentsCoool one man...
Thx.....
Really nice, thank you.
Really funny tutorial and a great lesson also.
Thanks! ^^
Excellent tutorial. Great analogy made it easy for me to understand
Great tutorial...thanks
Last one stands for me too. :D
Related Tutorials
Related Source Code
C++ Job Search