Do I need to learn C before C ++?

  • Thread starter Thread starter Amine Quentin
  • Start date Start date
AI Thread Summary
Learning C before C++ is not a requirement, but it can be beneficial for understanding foundational programming concepts. C++ builds on C's syntax while introducing object-oriented programming features that enhance code organization and teamwork for larger projects. Beginners can start with C++ directly, focusing on procedural programming before tackling OOP concepts. However, grasping C can provide valuable insights into low-level programming and memory management, which are useful in specific contexts like system programming. Ultimately, the choice of whether to learn C first depends on individual goals and the complexity of projects being undertaken.
  • #51
Stephanus said:
Since C source codes can be compiled by C++ compiler. There's nothing wrong with learning C++.
You can learn C++ without OOP and operator overloading and function overloading and it's like C before you know you compile it by C++ compiler. As @rcgldr says,
No, it's not "like" C when you do that, it IS C.
 
Technology news on Phys.org
  • #52
phinds said:
No, it's not "like" C when you do that, it IS C.
Impressive isn't it. It's two entirely different languages yet very much alike.
 
  • #53
Stephanus said:
Impressive isn't it. It's two entirely different languages yet very much alike.
I don't understand what you are talking about. What do you mean they are "two entirely different languages" ? C++ is just OOP put on top of C.
 
  • #54
phinds said:
I don't understand what you are talking about. What do you mean they are "two entirely different languages" ? C++ is just OOP put on top of C.
You are right, C++ supersedes C but on one hand a very large number of core libraries developed during the time C came along are heavily dependent on C. Industrial and business needs are, on the other hand, relying on the management of time frame and budget, so all of a sudden people can't take risk to upgrade their systems to latest technologies that use C++ or the likes and at the same time security issues have also been brought up a lot like plagues or pathogens the human have to fight against.
C developers thus have to keep its mainstream C alive while simultaneously developing C++ language. Almost all seniors understanding C also can code in C++ but the reverse case for the young people may not be true.
 
  • #55
I feel very strongly about this. The fact that C++ is C is meaningless (although it is true). When you go into ANY C++ class there will be assumptions about how much C you know. Please read my example above regarding building a linked list class. And it isn't like the C++ class is going to spend time teaching everyone about C basics like pointers, arrays, and the like.

If you talk to the professor and he specifically says that he will teach C++ beginning with the assumption that the students have zero C knowledge, then possibly I'll relent. But, even then, you will learn more if you know C going in.

Again, the fact that C++ is based on C is meaningless in this context. The C++ constructs are built on top of (out of) C, and you should understand basic C going in. You use a lot of basic C in any C++ program.
 
  • #56
meBigGuy said:
I feel very strongly about this. The fact that C++ is C is meaningless (although it is true). When you go into ANY C++ class there will be assumptions about how much C you know. Please read my example above regarding building a linked list class. And it isn't like the C++ class is going to spend time teaching everyone about C basics like pointers, arrays, and the like.

If you talk to the professor and he specifically says that he will teach C++ beginning with the assumption that the students have zero C knowledge, then possibly I'll relent. But, even then, you will learn more if you know C going in.

Again, the fact that C++ is based on C is meaningless in this context. The C++ constructs are built on top of (out of) C, and you should understand basic C going in. You use a lot of basic C in any C++ program.
You are making exactly the point I have been propounding all along. It seems to me that you are agreeing with me while disagreeing with me.

You say "The C++ constructs are built on top of (out of) C, and you should understand basic C going in" which is exactly what I mean when I say that C++ is just C plus the OOP constructs.
 
  • #57
phinds said:
You are making exactly the point I have been propounding all along. It seems to me that you are agreeing with me while disagreeing with me.

You say "The C++ constructs are built on top of (out of) C, and you should understand basic C going in" which is exactly what I mean when I say that C++ is just C plus the OOP constructs.
Not merely C plus OOP, but the operator overloading is a very new concept.
Of course you can't make operator overloading without class definition.
cin and cout are the most common objects.
 
  • #58
Stephanus said:
Not merely C plus OOP, but the operator overloading is a very new concept.
Of course you can't make operator overloading without class definition.
cin and cout are the most common objects.
I consider overloading to be part of the OOP constructs.
 
  • #59
phinds said:
I consider overloading to be part of the OOP constructs.
But in Delphi, there's no operator overloading. OOP doesn't have to have operator overloading. But, you can't do operator overloading without OOP. Furthermore in C++ there is function overloading feature, right. Doesn't have anything to do with OOP.
Although I use Delphi, but I'm very impressed with C++.
I don't have to write this, you must have known it, just to give you the idea.
In C++, we can write.
Vector + Vector = Vector
Vector - Vector = Vector
Coordinate + Vector = Coordinate
Matrix * Number = Matrix

and we can overrule these
Vector + Coordinate = n/a

I bet, Dennis Ritchie (?), when he developed C++ he must have thought that any programmer who wants to code in C++ must have sufficient knowledge in mathematic, logic as well as programming language.
 
  • #61
meBigGuy said:
If you talk to the professor and he specifically says that he will teach C++ beginning with the assumption that the students have zero C knowledge, then possibly I'll relent.

For several years (roughly 1995-2003 IIRC), I taught a two-semester introductory college computer science course that used C++. I did not assume that students knew any C beforehand, or indeed any other programming language. Many other colleges and universities had similar courses. During this period, C++ was also the preferred language for AP computer science courses in US high schools.

Around 2005, the College Board changed the AP computer science course from C++ to Java, and many colleges and universities (including us) followed suit with their intro CS courses.

Even though there is less demand for them now, there are still some introductory C++ textbooks for such courses, for example:

https://www.amazon.com/dp/0133378713/?tag=pfamazon01-20

https://www.amazon.com/dp/0763771562/?tag=pfamazon01-20

I used this book which is no longer in print:

A Computer Science Tapestry: Exploring Computer Science with C++ (Astrachan)
 
Last edited by a moderator:
  • #62
Stephanus said:
Sorry, it's Bjarne Stroustrup
https://en.wikipedia.org/wiki/C++
Who is Dennis Ritchie? I have his book.
Wait..
Ah there he is: https://en.wikipedia.org/wiki/Dennis_Ritchie
Ahh, died in 2011. At the same time as Steven Jobs as I recall.
RIP Dennis Ritchie...
Stroustrup created C++ on top of C. That's why I keep saying that C++ is just C plus OOP stuff (and as you seem conflicted about, but are clearly aware of, overloading only comes with the OOP stuff).
 
  • #63
Stephanus said:
Sorry, it's Bjarne Stroustrup
https://en.wikipedia.org/wiki/C++
Who is Dennis Ritchie? I have his book.
Dennis Ritchie is one-half of the acronym "K & R" which is very well-known to all long-time C programmers. Brian Kernighan (the 'K' of K & R) and Dennis Ritchie (the 'R' of K & R) wrote "The C Programming Language" back when they worked at Bell Labs.
Stephanus said:
Wait..
Ah there he is: https://en.wikipedia.org/wiki/Dennis_Ritchie
Ahh, died in 2011. At the same time as Steven Jobs as I recall.
RIP Dennis Ritchie...
 
  • #64
phinds said:
Stroustrup created C++ on top of C.

True. In fact, IIRC Stroustrup's working name for the language while he was developing it was "C with Classes."

That's why I keep saying that C++ is just C plus OOP stuff

At one time this was sort of true, to some approximation. I remember reading, while I was learning C++ around 1995, that there was some C code that most C++ compilers did not accept. I think there may have been an FAQ about in the Usenet newsgroup comp.lang.c++. My understanding is that after the first C++ standard came out in 1998(?) C and C++ started to diverge further from each other. Later versions of C added features which were not added to C++, and vice versa.
 
  • #65
Isn't it interesting. I bet all the mentors/staffs here are very capable and fluently coding in C or C++. But it seems that we argue what is C relative to C++.
But I am not astonished, because only in C do the programming language has its fanatics.
I have (had?, don't know where the book now) a book. It's "Born to Code in C". The author is Herbert Schildt.
There is a semaphore programming in his books. And if I'm not mistaken, there is a simple compiler, or there isn't.
But C is an interesting P/L. Never do I find it in Pascal, assembly.
Much less Clipper :headbang:
 
  • #66
And for the OP question.
"Do I need to learn C before C++"
I think my answer is this.
If you don't know about OOP, then even if you compile your code by C++ compiler, then it's C, As @phinds says.
And if you don't use function overloading, and class definition, then it's C.
If I recall correctly, in C++ we can even put methods in union and struct, not just class. I don't know about enum. Can we do that in Enum?
 
  • #67
phinds said:
Stroustrup created C++ on top of C. That's why I keep saying that C++ is just C plus OOP stuff (and as you seem conflicted about, but are clearly aware of, overloading only comes with the OOP stuff).
To use operator overloading, you need OOP.
Function overloading does not need OOP.
And OOP does not depend whether the language can do function overloading or not.
C++ can have multiple inheritances, but C++ can't have two constructors without function overloading. Pascal/Delphi can have more than one constructors per class. But in my 20 years of Delphi programming, I never use two constructors.
 
  • #68
Stephanus said:
But I am not astonished, because only in C do the programming language has its fanatics.
I have (had?, don't know where the book now) a book. It's "Born to Code in C". The author is Herbert Schildt.
There is a semaphore programming in his books. And if I'm not mistaken, there is a simple compiler, or there isn't.
I'm pretty sure Herb Schildt didn't include a compiler with any of his books.

Stephanus said:
And if you don't use function overloading, and class definition, then it's C.
If I recall correctly, in C++ we can even put methods in union and struct, not just class. I don't know about enum. Can we do that in Enum?
I'm going to say that you definitely can't put functions/methods in an enumeration (why would you want to?), and I don't think you can put them in a union. If you can, I've never seen an example of it.

You can have C structs with member functions -- by using function pointers.
 
  • #69
Mark44 said:
I'm pretty sure Herb Schildt didn't include a compiler with any of his books.I'm going to say that you definitely can't put functions/methods in an enumeration (why would you want to?), and I don't think you can put them in a union. If you can, I've never seen an example of it.

You can have C structs with member functions -- by using function pointers.
It's been 20 years since I learn C++. I'm using C now. But I remembered something like "member function in union"
http://stackoverflow.com/questions/5275100/union-in-c-are-they-feasible
Code:
template<typename T>
unionFoo{
public:
  Foo(){}
  Foo(const T& value): _val(value){}

  constchar* data()const{
    return _tab;
  }

  std::size_t size()const{
    returnsizeof(T);
  }

  charoperator[](unsignedint index)const{
      return _tab[index];
  }

private:
  T _val;
  char _tab[sizeof(T)];
}
Yes, member function in enum really doesn't make any sense.
 
  • #70
Mark44 said:
...
You can have C structs with member functions -- by using function pointers.
No, it's not that we have a function pointer element in a struct. But it's a real method.
Remember, that class without methods works like struct. Just member variable.
But in struct, you can't derive it, you can't inherit it. And I'm pretty sure there are no virtual/dynamic method in struct. Frankly, I never used method in struct before. I just read in the C++ book, that it can do that. I only use class and played around with operator overloading.
 
  • #71
phinds said:
You are making exactly the point I have been propounding all along. It seems to me that you are agreeing with me while disagreeing with me.

No, I'm not reacting to you at all. The fact that C++ is built on top of C is not at issue (at least not for me). It's whether you learn the C basics before learning the higher level constructs. I'm a strong proponent for learning the basics first, but as has been mentioned there are courses that explicitly state that no C knowledge is required.

In any C++ course a person who already knows C will always have an easier time of it than a person who does not.
 
  • #72
Stephanus said:
No, it's not that we have a function pointer element in a struct. But it's a real method.
The member functions in a C++ class are typically implemented as pointers, same as what I was talking about.
Stephanus said:
Remember, that class without methods works like struct. Just member variable.
But in struct, you can't derive it, you can't inherit it.
You can simulate derivation by having a struct member that is itself a struct.
Stephanus said:
And I'm pretty sure there are no virtual/dynamic method in struct.
Of course, you can't get all of the OOP features of C++ in C, because C is roughly a subset of C++.
Stephanus said:
Frankly, I never used method in struct before. I just read in the C++ book, that it can do that. I only use class and played around with operator overloading.
 
  • #73
Mark44 said:
The member functions in a C++ class are typically implemented as pointers, same as what I was talking about.
You mean in Test1 or Test2?
Code:
class Test1 {
   public: 
     int MyVar;
     int MyFunction(int b) { return(b*10); }
};

class Test2 {
   public:
     int MyVar;
     int (*MyFunction) (int);
};

int main()
{
  Test1 Test1Var[1000]; 
  Test2 Test2Var[1000];
}
Now, I'd say that Test2Var takes up more memory then Test1Var, IF we implement the member function as pointer.
 
  • #74
Stephanus said:
You mean in Test1 or Test2?
I mean in both.
Stephanus said:
Code:
class Test1 {
   public:
     int MyVar;
     int MyFunction(int b) { return(b*10); }
};

class Test2 {
   public:
     int MyVar;
     int (*MyFunction) (int);
};

int main()
{
  Test1 Test1Var[1000];
  Test2 Test2Var[1000];
}
Now, I'd say that Test2Var takes up more memory then Test1Var, IF we implement the member function as pointer.
I wouldn't say that. I would bet that both classes are exactly the same size.
 
  • #75
Mark44 said:
I mean in both.

I wouldn't say that. I would bet that both classes are exactly the same size.
I think Test2Var takes up more space.
We just can't sizeof(a class). If we sizeof it, it would return 4, the size of the pointer. But...
Test1::MyFunction is a method
Test2->MyFunction is a variable, which is a pointer of a method.
In a class, you can new 1000 objects of that class. Only the variables are created. But there is only 1 method.
In Test2, since MyFunction is a variable it's included in every objects that are created.
We define a class, we create an object.
 
  • #76
Stephanus said:
I think Test2Var takes up more space.
I compiled your code (VS 2013), and a Test2 instance does take up more space, at least the MSFT does it, which is probably an implementation detail. The last time I checked, which was about 20 years ago, I believe that they implemented class methods as pointers to code, but they don't seem to do that any more.
Stephanus said:
We just can't sizeof(a class). If we sizeof it, it would return 4, the size of the pointer.
But you can take sizeof an instance of a class.
 
  • #77
Mark44 said:
I compiled your code (VS 2013), and a Test2 instance does take up more space, at least the MSFT does it, which is probably an implementation detail.
How do you know? You increase the size of the array say.. 1000000 elements, and see the remaining memory in your computer?
And Mark, I forgot something. IF you define Test1::MyFunction as virtual not as static, I think it Test1 Test1Var[10000], will take some memory, because the compiler needs to store its address. But that's how it works in Pascal. I think the same goes to C++, too.

Mark44 said:
I The last time I checked, which was about 20 years ago, I believe that they implemented class methods as pointers to code, but they don't seem to do that any more.
Really?

Mark44 said:
But you can take sizeof an instance of a class.
In Pascal, we can't sizeof the instance of the class. How you do that in C?
MyClass *MyClassVar;
x = SizeOf(MyClassVar) will give you 4.
x = SizeOf(MyClassVar*), like this?
 
  • #78
Correction.
Stephanus said:
... IF you define Test1::MyFunction as virtual not as static, I think it Test1 Test1Var[10000], will take some memory, because the compiler needs to store its address. But that's how it works in Pascal. I think the same goes to C++, too.
No. It doesn't work that way.
The compiler doesn't have to store virtual method pointer in the class. The compiler stores it in the class definition. For each class (not object) that we define, the compiler reserved some memory for their VMT, virtual method table. And some static function pointer.
Test1Var[100000] still takes a little memory. The compiler does not store the address of MyFunc, even if it is a virtual method.
I guess so.
 
  • #79
Mark44 said:
I compiled your code (VS 2013), and a Test2 instance does take up more space, at least the MSFT does it, which is probably an implementation detail.
Stephanus said:
How do you know? You increase the size of the array say.. 1000000 elements, and see the remaining memory in your computer?
And Mark, I forgot something. IF you define Test1::MyFunction as virtual not as static, I think it Test1 Test1Var[10000], will take some memory, because the compiler needs to store its address. But that's how it works in Pascal. I think the same goes to C++, too.
I didn't bother creating an array of objects of each type, just one of each. My program used the sizeof operator to determine how many bytes were used by each of the two objects

Mark44 said:
I The last time I checked, which was about 20 years ago, I believe that they implemented class methods as pointers to code, but they don't seem to do that any more.
Stephanus said:
Really?
That's the way I remember it.

Mark44 said:
But you can take sizeof an instance of a class.
Stephanus said:
In Pascal, we can't sizeof the instance of the class. How you do that in C?
MyClass *MyClassVar;
x = SizeOf(MyClassVar) will give you 4.
x = SizeOf(MyClassVar*), like this?
Yes, except that the operator is sizeof in C/C++
 
  • Like
Likes Stephanus
  • #80
Mark44 said:
I didn't bother creating an array of objects of each type, just one of each. My program used the sizeof operator to determine how many bytes were used by each of the two objects.
I think the result of A=sizeof(ClassType*) could be misleading. There are additional variables involved.
Virtual Method Table.jpg

But that is in the declaration for each class definition not each class instance that the compiler generate.
And it's in Pascal. Perhaps C compiler generates some table, too for each of their class definition declared.
Mark44 said:
Yes, except that the operator is sizeof in C/C++
Come on, I've been doing Pascal 90% than C. And in your later years as a progammer, you wouldn't use sizeof anymore. You might have known the size of the variables you once define them.
I think learning C in C++ compiler is Ok. As long as the beginner doesn't go straight to OOP.
I'm impressed with @jtbell 's approach.
jtbell said:
I didn't do pointers (or OOP [added by me]) until near the end of the course...

... I remember having trouble ... to motivate introducing pointers. "OK, here's what I'd like to do, but none of the tools we've learned so far can accomplish it. So here's something new: pointers. (or OOP [added by me])"
 
  • #81
Stephanus said:
And in your later years as a progammer, you wouldn't use sizeof anymore. You might have known the size of the variables you once define them.
Oops, sorry.
Code:
#define ArrSize 100
int a[ArrSize];
int *p;
p=malloc(ArrSize*sizeof(int)); // much better than
p=malloc(ArrSize*4);
It's a good discussion we have. Add more insights for me.
 
Last edited:
  • #82
The original question posed by this thread has been answered. The thread is now veering around in different directions related to C versus C++ with some Pascal thrown in. (I admit to being guilty of contributing to this.) This thread is now closed. If someone wants to continue one of these veered-off discussions in a new thread with a focused title, please feel free to do so.
 
Last edited:
Back
Top