Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making Classes is complulsory in C++ programming

Status
Not open for further replies.

mikesyd

Programmer
Nov 30, 2002
12
AU
Hi everyone,

I wonder that making Classes is compulsory in C++ programming or we just program like C language with C++ syntax? If it is compulsory then everytime defining an array do we have to make Constructor, Destructor and Resize methods according to C++ Object Oriented programming?

Thanks in advance for your reply!
 
You can use C++ any way you want. You can speak C++ with a "C accent" like you describe. You can use it for OO. You can use it for template metaprogramming. You can use it for all of these at once and for anything in between.

C++ is, quoting its designer/developer, "a multi-paradigm programming language." That means it's designed to support a wide range of programming styles, including procedural, functional, object-oriented, and many more. (Although it does a better job with some of these than others).

So if you want to use C++ as a kind of C with some extra little goodies like references, function overloading, and templates, go right ahead. You aren't obligated to use classes for everything (that would be Java :) ). You only use them where you need them.

However, if you do use C++ as a glorified C, know that you are only using a small subset of its power.


With your array question, I'm not sure I understand where you're coming from.

You're completely capable of using regular C-style arrays in C++. As they are built-in types, you don't need (or get) to define constructors and methods for them.

If you define an array class, it would probably be nice to include those methods, since your reason for defining the class is that you or someone else is going to subsequently use it.

However, you say "every time you define an array." If you define an array class at all, you probably only do it once, unless you have several highly specialized reasons for doing so. Once you create an array class, you can use it in other programs as well; you should only need to define it once and possibly come back and modify it later.

Of course, unless you have a really good reason for creating your own array class, you should just use the std::vector class template. It's probably better and more efficient than anything you could write, since a library implementor wrote it; plus it's already there - don't reinvent the wheel.
 
Hi chipperMDW,

Oh very good very good to have your reply in great length and details. I honestly thank you! Regarding my question and array issue - you said "you aren't obligated to use classes for verything" and can use "as a kind of C", yes we can program in that way for ourselves, but when doing a C++ project for employers and they say programming in C++, do they STRICKLY generally speak programming in a true sense of C++ that is Object Oriented method and REQUIRE us to define class or they just let's do in anyway, as a kind of C like you said?

About array,I learned OO in C++ at Uni and they teached me making an array class (very complicated with Resize, copy Constructor, Assignment methods) in my OO assignment that make me wonder why can't we just use a standard array!!!-yes we define once only and use many times after.
 
The beauty of OOD is that you don't have to rewrite code over and over again. If an array is out there, you can use it without the complication of rewriting it yourself. I suspect your school was either trying to give you a deeper understanding of arrays or have you write a more robust array class for later use (built-in arrays don't keep track of their own size, etc., so it makes sense to write a different, more powerful array class).

I'm not a professional programmer (yet), but I've tinkered with co-workers' code to write some programs to automate tasks at my current job, and something I noticed about his code is that it was written in more of a procedural style. Because of this, trying to reuse his code for my purposes was an absolute mess. I had to rewrite the code to intercept bits of data that his procedure extracted. If his code were neatly functionalized and written in more of an OOD style, it would have been a lot easier.

OOP isn't necessarily implemented by all programmers and companies, but it makes things a lot easier and faster if they practice OOP properly.

If you write your code with the idea that it will be reused and changed in the future, then you'll be lovin it in the long term.
 
Hi BoulderBum,

Thank you for your info. I feel abit more confident to apply for a C++ programming job as people do program C++ in procedural style for commercial projects , although I am still very weak at it. OOP method seems helpful in reusability of old codes and neatly organised, but it requires lot of time in defining of OO classes, and related issues of OOP methods such as Constructor, Destructor, Initialize, etc. Program is longer.

I find programming in C++ or C very hard and it can make us miserable in debugging. I have formal training in C++ and Java but am scared to take them on, tend to look for something easier,like VB or Web development. Still hard to decide -what should I do!!!?
 
I disagree about OO making programs longer. Typically, the use of constructors and destructors makes programs shorter. A constructor performs all initializations and does all error checking in one simple statement. A destructor does all the cleanup and release of resources without you having to say a word.

You're saying that writing the constructors and destructors makes a program bigger, but again, I think you're missing the point that you only have to write them once. In fact, lots of things you'll use are already in the Standard library or Boost or Loki or some other library. The fact that the code is written to be reusable means you get to reuse it.

Typically, a simple C++ program is shorter than its equivalent C program, since you can omit most of the error checking and cleanup. I've written C programs where 80% of the code was opening some files, doing error checking, then closing them. The program was only a few lines long. All that could have been accomplished in a couple lines of C++ code.

Also, C++'s function objects and algorithms greatly reduce the ammount of code. Instead of writing a loop to do some really simple task, it's often possible to just call the transform function or something similar, and it'll go through the loop for you.

If you use it right, C++ works to reduce the ammount of code you end up writing. I think that, aside from actually getting work done, is the principle goal of programming. Programmers should strive to write their code in such a way that they and others can reuse it later. I call it being lazy ahead of time, or being selectively lazy.
 
Hi chipperMDW,

OO program is shorter than standard procedural programming style! - if that is so, I am glad to know. I don't really know well at all since haven't written many programs yet, but you may be right if the program is big, however for small simple program, I still think standard procedural is easier and shorter (no?)- you don't have to go through all the steps of defining in OO methods to carry out the logic of the program. I hope you are completely right and that I need to strenghten my skill in OO before comfortably agree with you. :):)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top