Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

What's the main difference between C and C++?

SchevenScheven Posts: 24Member
And would it be wise for someone to learn C++ coming straight out of Pascal? Thanks.
«1

Comments

  • MT2002MT2002 Posts: 1,444Member
    : And would it be wise for someone to learn C++ coming straight out of
    : Pascal? Thanks.
    :

    classes. C++ has built-in support for OOP, including multiple inheritance, polymorphism, encapsulation of objects. Better type safety. Templates. [b]const[/b] instead of the old C [b]#define[/b]. It supports almost everything in C, so C code can be complied with a C++ complier.

    C++ is very expendable--you can add your own types and objects (inheriting from) the child class properties and routines. This allows you to not only add more features to your own classes, but the standard library as well.

    Operator overloading--similar to function overloading, but with operators. It allows you to develop routines for operations, so that you may use them in expressions. For example,
    [code]
    Matrix A; // Matrix is a class that defines routines for matrices
    Matrix B;

    return A+B; // Do to operator overloading, we can have this statement
    // add and return the resulting matrix...Very readable here
    [/code]

    Namespaces. They allow you to declare multiple global variables or objects within different namespaces. Very useful. This helps prevent "multiple declaration" errors.

    Built-in exception handling support.

    None of this is possible in standard C. (I do not know if implemented const into the new C standard though)

    There is much more that we can add here. All of this is implemented in C++, but not C, and marks several differences between the two.

    Keep in mind that C and C++, while related, are [b]different[/b] languages, and should not be used together. ie. "C with classes". Either use C or C++, NOT both.

    You will see many other differences. For example, lets look at a hello, world program...in C:
    [code]
    #include

    int main (void) {

    printf ("Hello, World!/n");
    return 0;
    }
    [/code]
    ...And C++:
    [code]
    #include

    int main () {

    std::cout << "Hello, World!" << std::endl;
    return 0;
    }
    [/code]

    Hope this helps!
    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • SchevenScheven Posts: 24Member
    : : And would it be wise for someone to learn C++ coming straight out of
    : : Pascal? Thanks.
    : :
    :
    : classes. C++ has built-in support for OOP, including multiple
    : inheritance, polymorphism, encapsulation of objects. Better type
    : safety. Templates. [b]const[/b] instead of the old C [b]#define[/b].
    : It supports almost everything in C, so C code can be complied with a
    : C++ complier.
    :
    : C++ is very expendable--you can add your own types and objects
    : (inheriting from) the child class properties and routines. This
    : allows you to not only add more features to your own classes, but
    : the standard library as well.
    :
    : Operator overloading--similar to function overloading, but with
    : operators. It allows you to develop routines for operations, so that
    : you may use them in expressions. For example,
    : [code]:
    : Matrix A; // Matrix is a class that defines routines for matrices
    : Matrix B;
    :
    : return A+B; // Do to operator overloading, we can have this statement
    : // add and return the resulting matrix...Very readable here
    : [/code]:
    :
    : Namespaces. They allow you to declare multiple global variables or
    : objects within different namespaces. Very useful. This helps prevent
    : "multiple declaration" errors.
    :
    : Built-in exception handling support.
    :
    : None of this is possible in standard C. (I do not know if
    : implemented const into the new C standard though)
    :
    : There is much more that we can add here. All of this is implemented
    : in C++, but not C, and marks several differences between the two.
    :
    : Keep in mind that C and C++, while related, are [b]different[/b]
    : languages, and should not be used together. ie. "C with classes".
    : Either use C or C++, NOT both.
    :
    : You will see many other differences. For example, lets look at a
    : hello, world program...in C:
    : [code]:
    : #include
    :
    : int main (void) {
    :
    : printf ("Hello, World!/n");
    : return 0;
    : }
    : [/code]:
    : ...And C++:
    : [code]:
    : #include
    :
    : int main () {
    :
    : std::cout << "Hello, World!" << std::endl;
    : return 0;
    : }
    : [/code]:
    :
    : Hope this helps!
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :
    Wow. Well, according to my teacher, C is a reccomended course for someone planning to take C++, but I know in order to learn C++ that I would have to unlearn syntax and whatnot from C so I was unsure as to which course I should take next semester. I almost feel as though C and C++ are both equally as difficult to learn for someone coming straight out of Pascal, and I know C++ is the "big dog" of programming languages at the moment, so I just wanted to make sure with multiple sources like you guys wether or not it would be a waste of time learning C before C++. Anyways, I appreciate your help, but still, which class should I take?
  • MT2002MT2002 Posts: 1,444Member
    Hello,

    : Wow. Well, according to my teacher, C is a reccomended course for
    : someone planning to take C++,

    This is debatable. Some say that learning C first, then C++ is easier. The C languages does have some things that can be used in C++, but are considered "bad practice" in C++, but valid and good in C.

    For example, This will work in both C and C++:
    [code]
    char a = (char)b;
    [/code]
    In C++, however, it is better (Do to type safety) to do this:
    [code]
    char a = static_cast(b);
    [/code]

    Another example is using the preprocessor to create constants. In C:
    [code]
    #define PI 3.14159
    [/code]
    This is valid in both C and C++, However PI has no data type associated with it. As such, C++ introduced and encourages the use of [b]const[/b] for this.

    Just because it works in both C and C++ does not make it correct to use. Using #define in the above is good in C, but is [b]bad programming practice[/b] to use in C++. In C++, we would do this instead:
    [code]
    const int PI = 3.14159;
    [/code]

    : but I know in order to learn C++ that
    : I would have to unlearn syntax and whatnot from C

    No you don't. All syntax (with some minor exceptions) will be valid in C++.

    : like you guys wether or not it would be a
    : waste of time learning C before C++. Anyways, I appreciate your
    : help, but still, which class should I take?
    :

    I personally recommend learning C++ instead of C. Because C++ is a superset of C, you will also be learning C. The only difference is that you would be focusing on C++ and its standard library, not C's standard library.

    Its really up to you though. If you like, [link=http://www.cplusplus.com/doc/]HERE[/link] is a nice tutorial that you can look at, along with [link=http://www.cprogramming.com/]THIS[/link] page.

    Hope this helps!

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • SchevenScheven Posts: 24Member
    : Hello,
    :
    : : Wow. Well, according to my teacher, C is a reccomended course for
    : : someone planning to take C++,
    :
    : This is debatable. Some say that learning C first, then C++ is
    : easier. The C languages does have some things that can be used in
    : C++, but are considered "bad practice" in C++, but valid and good in
    : C.
    :
    : For example, This will work in both C and C++:
    : [code]:
    : char a = (char)b;
    : [/code]:
    : In C++, however, it is better (Do to type safety) to do this:
    : [code]:
    : char a = static_cast(b);
    : [/code]:
    :
    : Another example is using the preprocessor to create constants. In C:
    : [code]:
    : #define PI 3.14159
    : [/code]:
    : This is valid in both C and C++, However PI has no data type
    : associated with it. As such, C++ introduced and encourages the use
    : of [b]const[/b] for this.
    :
    : Just because it works in both C and C++ does not make it correct to
    : use. Using #define in the above is good in C, but is [b]bad
    : programming practice[/b] to use in C++. In C++, we would do this
    : instead:
    : [code]:
    : const int PI = 3.14159;
    : [/code]:
    :
    : : but I know in order to learn C++ that
    : : I would have to unlearn syntax and whatnot from C
    :
    : No you don't. All syntax (with some minor exceptions) will be valid
    : in C++.
    :
    : : like you guys wether or not it would be a
    : : waste of time learning C before C++. Anyways, I appreciate your
    : : help, but still, which class should I take?
    : :
    :
    : I personally recommend learning C++ instead of C. Because C++ is a
    : superset of C, you will also be learning C. The only difference is
    : that you would be focusing on C++ and its standard library, not C's
    : standard library.
    :
    : Its really up to you though. If you like,
    : [link=http://www.cplusplus.com/doc/]HERE[/link] is a nice tutorial
    : that you can look at, along with
    : [link=http://www.cprogramming.com/]THIS[/link] page.
    :
    : Hope this helps!
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :
    Awesome. That really helps, and thanks alot!
  • bilderbikkelbilderbikkel Posts: 754Member
    : Wow. Well, according to my teacher, C is a reccomended course for
    : someone planning to take C++, but I know in order to learn C++ that
    : I would have to unlearn syntax and whatnot from C so I was unsure as
    : to which course I should take next semester. I almost feel as though
    : C and C++ are both equally as difficult to learn for someone coming
    : straight out of Pascal, and I know C++ is the "big dog" of
    : programming languages at the moment, so I just wanted to make sure
    : with multiple sources like you guys wether or not it would be a
    : waste of time learning C before C++. Anyways, I appreciate your
    : help, but still, which class should I take?
    :

    Well, according to Bjarne Stroustrup and the authors of C++ FAQ LITE, they believe one should not learn C before C++ (see http://www.codepedia.com/1/LearnCbeforeCpp), whom I believe better then your teacher (unless his name is Scott Meyers or Herb Sutter, who would never state this).

    One can easily program 'C-like' (i.e. without making your own classes) in C++.

    See ya,
    bilderbikkel
  • LundinLundin Posts: 3,711Member
    M_PI in math.h is perhaps defined through the pre-processor, but it will be treated just as if you wrote 3.14 in the middle of the code, ie as a "const double" variable. Everything in C as well as C++ has a type.

    Regarding encapsulation, C has full support for it.

    C encapsulation
    [code]/* h-file */

    extern int get_x (void);[/code]

    [code]/* c-file */
    #include "hfile.h"

    static int x;

    int get_x (void)
    {
    return x;
    }[/code]

    [code]/* main */
    #include "hfile.h"

    int main()
    {
    int something = get_x();
    }[/code]


    C++ encapsulation
    [code]/* h-file */

    class SomeClass
    {
    private:
    int x;

    public:
    int get_x();
    };[/code]

    [code]/* c-file */
    #include "hfile.h"

    int SomeClass::get_x()
    {
    return x;
    }[/code]

    [code]/* main */
    #include "hfile.h"

    int main()
    {
    SomeClass sc;
    int something = sc.get_x();
    }[/code]


    So there is no big difference between the languages when it comes to encapsulation, that's a myth.


    : I personally recommend learning C++ instead of C. Because C++ is a
    : superset of C, you will also be learning C. The only difference is
    : that you would be focusing on C++ and its standard library, not C's
    : standard library.

    I would recommend C++ as well. Though note that it is a much larger and much more complex language than C, although a bit more modern and powerful.

  • MT2002MT2002 Posts: 1,444Member
    : M_PI in math.h is perhaps defined through the pre-processor, but it
    : will be treated just as if you wrote 3.14 in the middle of the code,
    : ie as a "const double" variable. Everything in C as well as C++ has
    : a type.

    Very true.

    What I was referring to was how the preprocessor #define is nothing more then text replacement, and not an actual type-named variable.

    There are some good examples that explain why #define is bad for constants (I cant think of any atm, though - busy day.)

    : Regarding encapsulation, C has full support for it.
    :
    : C encapsulation
    : [code]: /* h-file */
    :
    : extern int get_x (void);[/code]:
    :
    : [code]: /* c-file */
    : #include "hfile.h"
    :
    : static int x;
    :
    : int get_x (void)
    : {
    : return x;
    : }[/code]:
    :
    : [code]: /* main */
    : #include "hfile.h"
    :
    : int main()
    : {
    : int something = get_x();
    : }[/code]:
    :
    :
    : C++ encapsulation
    : [code]: /* h-file */
    :
    : class SomeClass
    : {
    : private:
    : int x;
    :
    : public:
    : int get_x();
    : };[/code]:
    :
    : [code]: /* c-file */
    : #include "hfile.h"
    :
    : int SomeClass::get_x()
    : {
    : return x;
    : }[/code]:
    :
    : [code]: /* main */
    : #include "hfile.h"
    :
    : int main()
    : {
    : SomeClass sc;
    : int something = sc.get_x();
    : }[/code]:

    Again, very true :-) and a nice example as well. C++ has built-in support for encapsulation through private members. (Although it does not "hide this data", as anyone can see it through the class interface, it still cannot be directly access or even touch the private members.)

    C can be used to provide interfaces and even OOP if done correctly.

    I guess the only difference here is, while both concepts - OOP and encapsulation -- can be implemented in both C and C++, C++ has built-in support for them.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • SchevenScheven Posts: 24Member
    : : Wow. Well, according to my teacher, C is a reccomended course for
    : : someone planning to take C++, but I know in order to learn C++ that
    : : I would have to unlearn syntax and whatnot from C so I was unsure as
    : : to which course I should take next semester. I almost feel as though
    : : C and C++ are both equally as difficult to learn for someone coming
    : : straight out of Pascal, and I know C++ is the "big dog" of
    : : programming languages at the moment, so I just wanted to make sure
    : : with multiple sources like you guys wether or not it would be a
    : : waste of time learning C before C++. Anyways, I appreciate your
    : : help, but still, which class should I take?
    : :
    :
    : Well, according to Bjarne Stroustrup and the authors of C++ FAQ
    : LITE, they believe one should not learn C before C++ (see
    : http://www.codepedia.com/1/LearnCbeforeCpp), whom I believe better
    : then your teacher (unless his name is Scott Meyers or Herb Sutter,
    : who would never state this).
    :
    : One can easily program 'C-like' (i.e. without making your own
    : classes) in C++.
    :
    : See ya,
    : bilderbikkel
    Thanks! That site was exaxtly what I was looking for. So, how different is C++ from Pascal if you know?
  • SchevenScheven Posts: 24Member
    : M_PI in math.h is perhaps defined through the pre-processor, but it
    : will be treated just as if you wrote 3.14 in the middle of the code,
    : ie as a "const double" variable. Everything in C as well as C++ has
    : a type.
    :
    : Regarding encapsulation, C has full support for it.
    :
    : C encapsulation
    : [code]: /* h-file */
    :
    : extern int get_x (void);[/code]:
    :
    : [code]: /* c-file */
    : #include "hfile.h"
    :
    : static int x;
    :
    : int get_x (void)
    : {
    : return x;
    : }[/code]:
    :
    : [code]: /* main */
    : #include "hfile.h"
    :
    : int main()
    : {
    : int something = get_x();
    : }[/code]:
    :
    :
    : C++ encapsulation
    : [code]: /* h-file */
    :
    : class SomeClass
    : {
    : private:
    : int x;
    :
    : public:
    : int get_x();
    : };[/code]:
    :
    : [code]: /* c-file */
    : #include "hfile.h"
    :
    : int SomeClass::get_x()
    : {
    : return x;
    : }[/code]:
    :
    : [code]: /* main */
    : #include "hfile.h"
    :
    : int main()
    : {
    : SomeClass sc;
    : int something = sc.get_x();
    : }[/code]:
    :
    :
    : So there is no big difference between the languages when it comes to
    : encapsulation, that's a myth.
    :
    :
    : : I personally recommend learning C++ instead of C. Because C++ is a
    : : superset of C, you will also be learning C. The only difference is
    : : that you would be focusing on C++ and its standard library, not C's
    : : standard library.
    :
    : I would recommend C++ as well. Though note that it is a much larger
    : and much more complex language than C, although a bit more modern
    : and powerful.
    :
    :
    Thank you!
  • SchevenScheven Posts: 24Member
    : M_PI in math.h is perhaps defined through the pre-processor, but it
    : will be treated just as if you wrote 3.14 in the middle of the code,
    : ie as a "const double" variable. Everything in C as well as C++ has
    : a type.
    :
    : Regarding encapsulation, C has full support for it.
    :
    : C encapsulation
    : [code]: /* h-file */
    :
    : extern int get_x (void);[/code]:
    :
    : [code]: /* c-file */
    : #include "hfile.h"
    :
    : static int x;
    :
    : int get_x (void)
    : {
    : return x;
    : }[/code]:
    :
    : [code]: /* main */
    : #include "hfile.h"
    :
    : int main()
    : {
    : int something = get_x();
    : }[/code]:
    :
    :
    : C++ encapsulation
    : [code]: /* h-file */
    :
    : class SomeClass
    : {
    : private:
    : int x;
    :
    : public:
    : int get_x();
    : };[/code]:
    :
    : [code]: /* c-file */
    : #include "hfile.h"
    :
    : int SomeClass::get_x()
    : {
    : return x;
    : }[/code]:
    :
    : [code]: /* main */
    : #include "hfile.h"
    :
    : int main()
    : {
    : SomeClass sc;
    : int something = sc.get_x();
    : }[/code]:
    :
    :
    : So there is no big difference between the languages when it comes to
    : encapsulation, that's a myth.
    :
    :
    : : I personally recommend learning C++ instead of C. Because C++ is a
    : : superset of C, you will also be learning C. The only difference is
    : : that you would be focusing on C++ and its standard library, not C's
    : : standard library.
    :
    : I would recommend C++ as well. Though note that it is a much larger
    : and much more complex language than C, although a bit more modern
    : and powerful.
    :
    :
    Thanks!
«1
Sign In or Register to comment.