difference between c and c++
Page 1 of 113 Replies - 1089 Views - Last Post: 15 February 2009 - 02:25 AM
#1
difference between c and c++
Posted 10 February 2009 - 01:13 PM
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
Replies To: difference between c and c++
#2
Re: difference between c and c++
Posted 10 February 2009 - 01:31 PM
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.
#3
Re: difference between c and c++
Posted 10 February 2009 - 02:16 PM
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
#4
Re: difference between c and c++
Posted 10 February 2009 - 02:16 PM
Seriously, how many sites go on and on about this?
#5
Re: difference between c and c++
Posted 10 February 2009 - 02:22 PM
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.
#6
Re: difference between c and c++
Posted 10 February 2009 - 04:42 PM
#7
Re: difference between c and c++
Posted 13 February 2009 - 01:20 AM
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
#8
Re: difference between c and c++
Posted 13 February 2009 - 01:24 AM
#9
Re: difference between c and c++
Posted 13 February 2009 - 08:34 AM
#10
Re: difference between c and c++
Posted 13 February 2009 - 08:46 AM
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.
}
#11
Re: difference between c and c++
Posted 13 February 2009 - 11:42 AM
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!
#12
Re: difference between c and c++
Posted 13 February 2009 - 02:17 PM
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.
#13
Re: difference between c and c++
Posted 14 February 2009 - 10:14 AM
#14
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 !
::
|
|

New Topic/Question
Reply




MultiQuote









|