• C

Difference between C and C++

I am taking a C++ class in the spring.  My background is creating databases using MS Access.  This will be my first real programming course, my question is:  What is the difference between C and C++??


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Simply put, the differences are mainly that:

1) C++ is designed more for object oriented programming via the use of classes - classes are like structs except that they encapsulate functions as well as data and provide access security levels for both. You will create instances of classes and call member functions of those classes. You will also learn about inheritance - how classes gain the attributes of other classes by deriving from them; and about polymorphism - this is the capability to treat objects from multiple classes identically because they all share one or more interfaces in common - for esample, Class Shape is some abstract class that cannot know how to draw itself, but if you derive a class from it called "Circle", class Circle does, and so would another class derived from Shape called "Square", etc...

2) C++ is a much more strongly typed language - the compiler will flag attempts to make implicit assignments from one type to another as opposed to just making automatic conversions, for example.

That's just a plain and simple starter, but it might give you some idea of what to expect...

There is no difference in the type checking between ANSI C and C++.
Yeah, you know, you may be right - it may not provide stronger type checking than straight C - maybe just too used to having to cast classes...



    To make this a little bit clearer to you, C is more of a top-down language where you use functions in order to accomplish tasks.  For instance lets say that you wanted to create a car and be able to do several things with the car such a turn right, turn left, stop...etc.  Within C you would probably call several different functions in order to do this task.  Like TurnRight(....), Stop(...), SlowDown(....) and so forth.  
     In C++ it is an object oriented language.  So instead of just creating several functions that are not grouped together or classified like C, you group all of your functions together to create an Object or a Class.  So in the example given of creating a car you would create a class possibly called Car where you would have all of the functions related to creating and functions for this car grouped in the class.
     The creation of a car is created by what is called a constructor.  A constructer is basically a function in your class that creates the object.  This function is called the same name as your class.  You also have to have a function that destroys your object.  This is called a destructer.  It is called the same name as your class but with a ~ in front of it.  A class definition looks like this:

class Car
         Car();  // Constructor
        ~Car();  // Deconstructor
         void TurnRight(....);
         void TurnLeft (....);
         int  NumLeftTurns(....);
         int num_leftTurns;
    As you can see in the class definition there are the words public and private.  This is where the security comes in play.  Public refers to variables and functions that are accessible to any item within the class and outside the class.  While private means that only the functions within the class will be able to use these functions or variable.  So for this example the variable num_leftTurns can only be used by the functions with in the class Car such as NumLeftTurns(...), TurnLeft(...) and so forth.
     With C you do not have this level of security.  As long as the functions are defined you would be able to use them anywhere.
     Once you have your class definition created in C++ you would then create your functions within the class.  For example...

    int Car::NumLeftTurns( )

      return num_leftTurns;

    You may be asking why we would not want to just give access to everyone with the num_leftTurns variable instead of place in in private and have a function from the Car class call this.  The reason is that we may not want the user to be able to change the number of left turns.  We may just want the functions within the Car class to keep track of it automatically.
    Once you have your class created which includes all of your functions you then create an instance of your class.  Which just means that you create an object that represents this class.  This would be called like this:

Car CarOne;

Once an instance of the class is created you can then use its functions by calling them like this:

int numLTurns = CarOne.NumLeftTurns( );

   In regards to inheritance C++ allows you to inherit functions of one class into another class.  For instance lets say that you now want to create a truck. As you know a truck has a lot of similarities with the functions of a car, thus keeping some of the functions that you all ready created for a car would be very helpful when trying to create a class for a truck.  In C++ you can do this by creating the class definition for a truck by the following:

class Truck : public Car

     Truck( );
    ~Truck( );
     bool HasATrailer(...);

So now you would create the constructor and the destructor for the object Truck.  You could then create its own functions that act independently of the car function.  You can also rewrite functions that are already in the Car class by just adding the function to the Truck definition and rewriting the code.  Once your Truck definition has been created you can call functions from both the Truck definition as well as the Car definition...

Truck truckOne;

   bool yesorno = truckOne.HasATrailer(...);
   int = truckOne.NumLeftTurns(... );

As you can see C++ is very object oriented and makes very good use or reusable code, while C does not have as many capabilities.  The syntax for C/C++ are pretty much the same besides a few changes which you will pick up in the course of your class. You can use C interchangably with C++ on any C++ compliler. If you know C the hardest challenge you will have is moving from the Top-down approach to the Object Oriented approach in C++.  Hope this helped.

Let me know if you have any more questions.

-Thanks :0)


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
At the deepest level, they are essentially the same, but in OOP functions an invisible parameter is passed (the object being used at the moment).

In C, where you do this:


you would do this in C++:


Both pass two parameters to the function, but in C++ the parameter is invisible (the object passes itself as a parameter, so you don't have to do it manually).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.