13 Replies - 1467 Views - Last Post: 15 February 2009 - 02:25 AM Rate Topic: -----

#1 mfaheemriaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-September 08

difference between c and c++

Post icon  Posted 10 February 2009 - 01:13 PM

hi
can n e one plz clear my concepts about difference in c and c++,,wat i know is dat c++ is an OOP language,,and i know dats true,,but i don't know y it is called OOP and C is not OOP,,
plz tell me by giving examples,,like c program in C and C++,,and difference between them

thanks
Is This A Good Question/Topic? 0
  • +

Replies To: difference between c and c++

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1148
  • View blog
  • Posts: 7,149
  • Joined: 07-September 06

Re: difference between c and c++

Posted 10 February 2009 - 01:31 PM

C only allows for structs to be used. But with C++ you can create instances of objects which allow you to do a lot more complicated things without having the code as cluttered. C++ allows for classes.

In C++ you can do things like so:
class test{
  public:
	test(){ // constructor also called contor
	  // stuff here
	}
	~test(){ // destructor also called detor
	  // stuff here
	}
};
int main(){
  test* myTest1 = new test(); // creates an object pointer
  test myTest2(); // creates an object
  return 0;
}


Hope that makes sense.
Was This Post Helpful? 0
  • +
  • -

#3 bodom658  Icon User is offline

  • Villiage Idiom
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: difference between c and c++

Posted 10 February 2009 - 02:16 PM

C can be thought of as a subset of C++, but C++ offers a vast quantity of improvements, including the introduction of object oriented programming.

Object Oriented Programming is as described by Beta. C was not object oriented, but procedural, in that you had functions that would carry out tasks upon different variables, and basically everything was a member of the same scope, besides the ocassional use of a struct. C++ allowed variables and functions to be "encapsulated" into classes, of which instances called objects could be created. These objects could then interact with eachother, and their data was protected from any miswritten function. Also, with this addition, databasing was greatly enhanced, as it became much easier to control and contain vast amounts of information in memory.

Google it for more info
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: difference between c and c++

Posted 10 February 2009 - 02:16 PM

C++ is like having a kitchen stocked with every automated device conceivable; a machine for every need, most as seen on TV. In C you only get a set of knives, but they're really sharp.

Seriously, how many sites go on and on about this?
Was This Post Helpful? 0
  • +
  • -

#5 ghst  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 16
  • Joined: 29-October 08

Re: difference between c and c++

Posted 10 February 2009 - 02:22 PM

One of the biggest differences between C and C++ is inheritance. It is true in C you can actually have inheritance but it is much different then C++.

Lets say you wanted to make health crate and weapon crate classes in c++ and c.

Well to do it in C++ you would go about making a base class that both use to define a basic crate. Then you would derive a health crate class and a weapon crate class off of that base class. Like follows.
class Crate
{
protected:
	int size; //dimensions of our crate!
public:
	Crate() : size(0)
	{
	}
	virtual ~Crate()
	{
	}

	virtual void Init()
	{
		size = 2;
	}
};

class HealthCrate : public Crate
{
public:
	HealthCrate()
	{
	}
	~HealthCrate()
	{
	}
	void Init()
	{
		size = 5;
	}
};

class WeaponCrate : public Crate
{
public:
	WeaponCrate()
	{
	}
	~WeaponCrate()
	{
	}
	void Init()
	{
		size = 10;
	}
};




Now to do this in C you would have to have a structure defined for crate, then health and weapon crate would both have to store an object of crate inside of them instead of in C++ where they are actually a crate. So this is just one small difference between c and c++, there are a lot more so I recommend doing some extensive reading to find out more information between the two.
Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: difference between c and c++

Posted 10 February 2009 - 04:42 PM

View Postbaavgai, on 10 Feb, 2009 - 02:16 PM, said:

C++ is like having a kitchen stocked with every automated device conceivable; a machine for every need, most as seen on TV. In C you only get a set of knives, but they're really sharp.


:^: Class, I like that analogy.

@OP
Objective-C has OOP elements.
Was This Post Helpful? 0
  • +
  • -

#7 mfaheemriaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-September 08

Re: difference between c and c++

Posted 13 February 2009 - 01:20 AM

well thanks all of you,, after reading all of yours posts i just understand that there is a difference of objects and classes in c and c++,,like we work in objects and classes in c++ while in c we do not,,indeed we can't,,,
but now i have some more questions,,,what are these classes and objects???
about objects i just know that objects are like pre-defined functions like,,int, float, default, etc etc or u can say those words which we can not use in our program,,for example if we write a word default then it will not work in that way in which we want but it will work for that specific task which had been already given to it by c or c ++

and i have no idea of classes,,

i am ashamed of my self that i didn't know the basics so i am sorry if i am asking you silly queations but i want to clear my concepts,
thanks

This post has been edited by mfaheemriaz: 13 February 2009 - 01:22 AM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5314
  • View blog
  • Posts: 27,220
  • Joined: 10-May 07

Re: difference between c and c++

Posted 13 February 2009 - 01:24 AM

View Postmfaheemriaz, on 13 Feb, 2009 - 02:20 AM, said:

but now i have some more questions,,,

No, those are commas.
Was This Post Helpful? 0
  • +
  • -

#9 mfaheemriaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-September 08

Re: difference between c and c++

Posted 13 February 2009 - 08:34 AM

sorry didn't get ur point,,will any one please explain little more
Was This Post Helpful? 0
  • +
  • -

#10 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3101
  • View blog
  • Posts: 19,141
  • Joined: 14-September 07

Re: difference between c and c++

Posted 13 February 2009 - 08:46 AM

Objects are not primitives (int, float, etc...)

Class:

class Cat{
//stuff
};



An object is an initiation of a class:

int main()
{
   Cat* Frisky = new Cat(); //Frisky is an object, Cat is the class
}



Hope that helps.
}
Was This Post Helpful? 0
  • +
  • -

#11 crazyjugglerdrummer  Icon User is offline

  • GAME OVER. NERD WINS.
  • member icon

Reputation: 119
  • View blog
  • Posts: 690
  • Joined: 07-January 09

Re: difference between c and c++

Posted 13 February 2009 - 11:42 AM

One objective of programming is to emulate the real world as closely as possible. THis involves making program equivalents of what exists in the real world. The best way to do ths is to create objects in programs as we have in the real world. So we can make a ball....
class ball
{
int size;

void bounce();
}


or more complicated things, like a car that is a vehicle and has a motor
class car: public vehicle
{
Motor& engine;

void drive();
}


objects make all of this possible as nothing else could. We would be lost without them.

There are plenty of tutorials on these.
Hope this explains it!
Was This Post Helpful? 0
  • +
  • -

#12 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,685
  • Joined: 16-October 07

Re: difference between c and c++

Posted 13 February 2009 - 02:17 PM

Objects are metaphorcial e.g. Cat class is a child of Animal class and has properties related to same. However, in a more practical sense, they're organizational. They allow all the logic relating to a given thing to become part of that thing. The thing only has to share as much information as need for other things to work with it.

I know, confusing as hell. Let's just go with organizational. Here's the skeleton of a Tic Tac Toe program.

#include<stdio.h>

#define BOARD_SIZE 9
#define COLS 3
#define ROWS 3
#define EMPTY_CELL ' '

int getBoardIndex(int row, int col) {
	return row*COLS+col;
}

void init(char board[]) {
	int i;
	for(i=0; i<BOARD_SIZE; i++) {
		board[i] = EMPTY_CELL;
	}
}

void show(char board[]) {
	int row, col;
	for(row=0; row<ROWS; row++) {
		if (row!=0) { printf("-+-+-\n"); }
		for(col=0; col<COLS; col++) {
			if (col!=0) { printf("|"); }
			printf("%c", board[getBoardIndex(row, col)]);
		}
		printf("\n");
	}
	printf("\n");
}

int move(char board[], int index, char player) {
	if (index<0 || index>=BOARD_SIZE) { return 0; }
	if (board[index]!= EMPTY_CELL) { return 0; }
	board[index] = player;
	return 1;
}

int main() {
	char b[BOARD_SIZE];
	init(B)/>;
	show(B)/>;
	move(b, 4,'X');
	show(B)/>;
	
	return 0;
}



It's reasonably well organized, but I still have to know what functions my board will use, what constants are associated with it, that I need to call init before I do anything.

Here's the same sort of thing in C++:
#include<stdio.h>

class Board {

public:
	static const int BOARD_SIZE = 9;
	static const int COLS = 3;
	static const int ROWS = 3;
	static const char EMPTY_CELL=' ';
	static const char INVALID_CELL = '*';
	static const char PLAYER1 = 'X';
	static const char PLAYER2 = 'O';

	Board() {
		for(int i=0; i<BOARD_SIZE; i++) {
			cells[i] = EMPTY_CELL;
		}
	}
	
	bool isValid(int index) const { return !(index<0 || index>=BOARD_SIZE); }
	
	char getValue(int index) const {
		if (isValid(index)) { return cells[index]; }
		return INVALID_CELL;
	}
	
	char getValue(int row, int col) const { return getValue(getBoardIndex(row, col)); }
	
	bool setValue(int index, char value) { 
		if (!isValid(index)) { return false; } 
		cells[index] = value;
		return true;
	}

	bool isAvailable(int index) const { return (getValue(index)== EMPTY_CELL); }
	
	bool move(int index, char player) {
		if (!isAvailable(index)) { return false; }
		setValue(index, player);
		return true;
	}
	
	void show() {
		for(int row=0; row<ROWS; row++) {
			if (row!=0) { printf("-+-+-\n"); }
			for(int col=0; col<COLS; col++) {
				if (col!=0) { printf("|"); }
				printf("%c", getValue(row, col));
			}
			printf("\n");
		}
		printf("\n");
	}
	
private:
	char cells[BOARD_SIZE];

	static int getBoardIndex(int row, int col) {
		return row*COLS+col;
	}
	
};

int main() {
	Board board;
	board.show();
	board.move(4, Board::PLAYER1);
	board.show();
	
	return 0;
}



This code got a little longer, but it has a lot more going on. The object initialized itself automatically, so the user doesn't have to worry about that. It also has a number of validation checks, so bad data can't get into the board. All the values that were just there in the C version now have hard associations with the class. Usage of the object is actually simpler, which is usually to goal.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#13 illuminati  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 6
  • Joined: 06-February 09

Re: difference between c and c++

Posted 14 February 2009 - 10:14 AM

c is use for low level projects (which must work with low level of protocol stack) and c++ is opposit.
Was This Post Helpful? 0
  • +
  • -

#14 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: difference between c and c++

Posted 15 February 2009 - 02:25 AM

::
:: C contains only C-Style Structure which can only contains
:: data and cannot be used for Data Structures ...
::
:: C++ there are structure, class, union, and other new things
:: like operators too and the whole OOP too !
::
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1