Qbasicnews.com

Full Version: C/C++ ??
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5 6 7 8 9
Zack, Nathan isn't always correct =P. Here is my arguement that C++ isnt just C + Classes:

- C does not have classes/objects! all code is in functions (subroutines).

- C structures can not have methods

- C I/O is based on library functions:
printf, scanf, fopen, fclose, fread, fwrite, … while C++ uses io streams.

- C does not support any function overloading (you can’t have 2 functions with the same name).

- C does not have new or delete, you use the messy malloc() and free() library functions to handle dynamic memory allocation/deallocation.

- C does not have reference variables

- Most of all C doesnt have the STL =P

And just for all the C fans out here ( that includes me =P) the best thing I love about C++ is the new() and delete() operators. Heres a self explanatory example:

p1 = new int; // using new
p1 = (int *)malloc (sizeof(int)); // using malloc

p2 = new employee; // using new
p2 = (struct employee *)malloc (sizeof(struct employee)); // using malloc

p3 = new int[30]; // using new
p3 = (int *)malloc(sizeof(int)*30); // using malloc

Hehe =P
Quote:- C does not have classes/objects! all code is in functions (subroutines).

What I said.

Quote:- C structures can not have methods

Structures with methods are classes. So what I said.

Quote:- C I/O is based on library functions:
printf, scanf, fopen, fclose, fread, fwrite, … while C++ uses io streams.

Those io streams are the overload of operators in a given class, so again... Anyhow, IO streams are a mess. You have more control with the good ol' functions.

Quote:- C does not support any function overloading (you can’t have 2 functions with the same name).

And you can do this only in classes. So again...

Quote:- C does not have new or delete, you use the messy malloc() and free() library functions to handle dynamic memory allocation/deallocation.

Because you are using classes. So yet again... classes.

Quote:- C does not have reference variables

Reference variables are like pointer to classes, so you know... Classes again.

Quote:- Most of all C doesnt have the STL =P

Not really.

Quote:And just for all the C fans out here ( that includes me =P) the best thing I love about C++ is the new() and delete() operators. Heres a self explanatory example:

p1 = new int; // using new
p1 = (int *)malloc (sizeof(int)); // using malloc

p2 = new employee; // using new
p2 = (struct employee *)malloc (sizeof(struct employee)); // using malloc

p3 = new int[30]; // using new
p3 = (int *)malloc(sizeof(int)*30); // using malloc

Hehe =P

That is creating objects made up with basic types. Again: classes (implicit).

Well, as you see, C ++ is C + classes. What it lacks? Polymorphism, interfaces, abstract stuff, etc.

Of course I am not always right. But this time I am. All your examples are basicly telling the same thing: "C++ has classes".

Hard Rock: Java has operator overloading. That's something that comes with OO. And all about constructors, deconstructors, templates and overloaded functions are "classes". You construct, make a template or overload a function in a class.
Ok, if everything is dealing with classes then why isnt abstraction, encapsulation, inheritance, polymorphism related with classes? Infact, they are methods of coding and not a part of C++'s functionality, though i facilitates it.

Gotcha!
Quote:And you can do this only in classes. So again...
Function overloading is verymuch possible *outside* classes =P

Quote:Reference variables are like pointer to classes, so you know... Classes again.

Wrong again! References are just like pointers but have a very subtle difference =P and you can very well make a reference to primary datatypes anot not just classes.

Quote:That is creating objects made up with basic types. Again: classes (implicit).

Dynamic allocation and deallocation isnt just about classes but also used for dy all/deall of primary/user defined/structs.

And yes C doesnt have a STL with vectors, linkedlists etc...(and yes linkedlists are used in C as well *without* using classes) =P
Quote:why isnt abstraction, encapsulation, inheritance, polymorphism related with classes?

I only mentioned polymorphism in my post. I did not mean abstraction. I meant abstract functions like:

Code:
public abstract int Foo(int a);

As I said, you are creating basic types like they were classes. When you do a = new int you are creating a reference to an integer object. Not only the objects that you define are objects but the basic types as well.

Function overloading is possible outside classes, but may lead to spaghetti code and messed up stuff, more if you are sharing modules coded by different programmers. When overloading was introduced (I believe it was in the ADA programming language, which used packages) it was done within the scope of a class. If you begin overloading outside a class it will lead you to errors and stuff. Nothing prevents you to define the same thing twice.

If C++ was a true OO language it didn't have pointers, just references.

If C++ was a true OO language it would start in a main object, not in a procedural function.

If C++ was a true OO language it would feature interfaces with several implementations (you can have for example a search method in an object defined in 10 different ways and change the one which is used on the fly).

If C++ was a true OO language it would feature abstract functions that may or may not be defined in a class but in his sons. That way you can apply four different design patterns of software engineering (factory method, for example). When you are working in a big group this is really needed.

If C++ was a true OO language everything should be a class. You would have a consistent clone method whenever you wanted to.

Anyhow, I know I am not wrong this time. 7 years at college made me learn what is a fully featured OO language and what is a Procedural+Classes language. If you are fine staying wrong, it's fine to me Big Grin I have nothing to prove. I still haven't found anything in C++ that is not C + classes.

PS. And I am talking about real projects involving many people. You in your bedroom won't get confused by some spaghetti outside classes and some nonsense overloads outside classes, but if your project is scheduled to have 500,000 lines of code and 25 people on it, you know. You better have things tied.
Incase you have forgotten, we arent arguing whether C++ is a true OO language. If you call Java a true OO language then so be it. It's messy and dumb to program in(Thats my opinion atleast).

And I think you should check out KDE's source code. It's coded in C++ =P and as you know they have more than 25 people developing it =). It's source code doesnt seem spagetti to me.
I am not on one or another side in this discussion, because I
only know C++ (and very little of it).

Na_th_an: You said that C++ doesn't have polymorphism right?
My C++ has a chapter about polymorphism, BUT I haven't read it
recently, or maybe at all, so it might be something I missed.
AFAIK polymorphism is when you have n subs with the same name
which takes different numbers/types of parameters and AFAIK
C++ has that, correct me if I'm wrong...
I mean polymorphism at a class level. You can have the same class defined in several ways and you chose the implementation you prefer depending on what you want it for. That's done with interfaces and abstract classes, which C++ lacks of.

And TBBQ, you may think what you like. I am not talking about what I think but about what I know, which is different. And in six months I'll be a computer engineer, so what I know is what it is. No opinions here, just facts. Loosecaboose is in my same position, I mean, he is finishing computer science as well, and he thinks like me. Sometimes it is better to believe and understand what years of researching say instead of making a wrong impression and defending it.

And KDE code is pure OO code, it doesn't have overloading outside objects and stuff like that. I am not saying that every C++ program is spaghetti, what I am saying is that the language lets you do spaghetti OO without complaining. I mean, in every C++ book and tutorial it should say "you can do this but don't do it".

And now Java is messy and dumb to program it. Can you explain why? Java is a cleanup of C++. How can a cleanup become messy? What's messy?

ADA, Scheme, Java, Eiffel and Objective C are good examples of OO languages. C++ is C + classes. Just like VB which is Basic + OO-alike-notation.
Dont take this post too seriously, I just like C coding:

Quote:C structures can not have methods
But they can have functions:
Code:
struct class {
  int (*method)(char *);
};

Quote:- C I/O is based on library functions:
printf, scanf, fopen, fclose, fread, fwrite, … while C++ uses io streams.
C uses IO streams too, such as stdin and stdout. The interfaces for them may not be the same, but they are streams.

Quote:- C does not support any function overloading (you can’t have 2 functions with the same name).
Too approachs to this one:
Code:
int overloadFunc(int arg, ...) {
  switch(arg) {
  case CHAR:
    /* Blah */
  case INT:
    /* Blah */
  default:
    /* Blah */
  }
}
Or:
Code:
int overLoadFunc(int arg1) {
  /* Func 1 */
}

int _overLoadFunc(int arg1, int arg2) {
  /* Func 2 */
}

#define overLoadFunc(arg1, arg2) _overLoadFunc(arg1, arg2)
Neither example does exactly what function overloading really does, but still allows the one function name to take different types and amounts of arguments.

Quote:- C does not have new or delete, you use the messy malloc() and free() library functions to handle dynamic memory allocation/deallocation.
You can always build wrappers for the "messy" malloc and free functions.

Quote:- C does not have reference variables
Not sure what you mean by this, how are reference variables different to pointers?

Quote:- Most of all C doesnt have the STL =P
No, but it does have the Standard C library.

Quote:And just for all the C fans out here ( that includes me =P) the best thing I love about C++ is the new() and delete() operators.
According to the C standard you dont need to cast the return value of malloc, your code only makes it look more complicated than it really is.
Code:
#define new(type, size) malloc(sizeof(type) * size)

p1 = new(int, 1);

typdef struct employee employee_t;
p2 = new(employee_t, 1);

p3 = new(int, 30);
Again, maybe not as "elegant" as C++, but wrapper functions can help avoid some of the common mistakes that occur when using malloc and free.
1) Give me an example of the use of function overloading.
2) How on earth are new and delete any better than malloc() and free()?
And Loose...I'll be looking into that struct method thing.
actoully, C do have function overloading, the C99 standard alowes it.
Pages: 1 2 3 4 5 6 7 8 9