Different B/W CLASSES & STRUCTUR in C\C++

Discussion of programming on Linux, including shell scripting, perl, python, c/c++, mono, java. Whatever tickles your fancy.
Post Reply
mani
Lance Naik
Posts: 33
Joined: Tue Aug 10, 2004 1:51 pm
Location: Islamabad
Contact:

Different B/W CLASSES & STRUCTUR in C\C++

Post by mani »

Hi 2 All,

i want 2 know that what is the major different in Structur and Class in C\C++ programming language. what r the Advantages and Disadvantages of then
outstream
Naib Subedar
Posts: 322
Joined: Wed Sep 24, 2003 10:04 pm
Location: islamabad

Post by outstream »

one of the biggest difference just came to mind is..classes support inheritence, structures dont.. is that a homework question by the way?
Testing?What's that? If it compiles, its good, if it boots up it is perfect.
----------------------------------------------------
Imran
Registered Linux User # 334322
saquib_javed
Naib Subedar
Posts: 344
Joined: Sat Apr 10, 2004 9:07 pm
Location: Karachi
Contact:

Post by saquib_javed »

Salam,

Please check for Object Oriented is. 8)

Do refer to book C How to program Deitel & Deitel
http://www.deitel.com/

http://webopedia.com/TERM/o/object_oriented.html

ThanX
Use Linux and feel Free.
OR
Feel free to use Linux. ;)
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: Different B/W CLASSES & STRUCTUR in C\C++

Post by lambda »

mani wrote:i want 2 know that what is the major different in Structur and Class in C\C++ programming language. what r the Advantages and Disadvantages of then
is this a homework question?
soni
Naik
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Location: Karachi
Contact:

struct

Post by soni »

structures do allow inheritance....

Code: Select all


#include <stdio.h>

struct father{

    father(){
       printf("In the father....\n");
    }  
    int scalar;  

};

struct child : father{

    child(){
       printf("In the child....\n");
       this->scalar = 20; 
    }  

};


main(void){

   struct child obj;

   printf("%d\n", obj.scalar);

}

struct{
//default member access....
public:
};

class{
//default member access....
private:
};
Please do not open M$Windoze.
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Re: struct

Post by lambda »

soni wrote:struct{
//default member access....
public:
};

class{
//default member access....
private:
};
that's correct -- soni wins.
salmankhilji
Cadet
Posts: 4
Joined: Tue Jan 27, 2004 7:38 am

Re: Different B/W CLASSES & STRUCTUR in C\C++

Post by salmankhilji »

mani wrote:Hi 2 All,

i want 2 know that what is the major different in Structur and Class in C\C++ programming language. what r the Advantages and Disadvantages of then
In C++, struct and class is EXACTLY the same semantically. Inheritance and polymorphism work EXACTLY the same on both.

The only difference is what is private and public be default. In a struct everything is public by default. However, there is nothing to stop you from specifying public: to make everything public. In a class, everything is private by default. However you can specify private: to achieve the same effect.

Some older compilers allow interchaning the keyword struct and class. i.e you can forward declare something to be struct and later define the thing as a class instead. These compilers, noticeably VC++ will generate a warning only. Other compilers simply will refuse to do so.

HTH,

Salman
ishtiaq ahmed
Naik
Posts: 50
Joined: Thu Dec 23, 2004 8:15 pm
Location: lahore

Post by ishtiaq ahmed »

actually brother u should have asked question like this, the difference between object oriented approach and the structural.

so basic difference between them is that oo approach is like a human eye that it is a object which contain attributes like ball and so on . and its function to. as in case of class object.

while in case of structural approach its not the case.


that is the very basic difference between them. i liked this example very much when i read that first time. so i am sharing it with u.
lambda
Major General
Posts: 3452
Joined: Tue May 27, 2003 7:04 pm
Location: Lahore
Contact:

Post by lambda »

ishtiaq ahmed wrote:so basic difference between them is that oo approach is like a human eye that it is a object which contain attributes like ball and so on . and its function to. as in case of class object.

while in case of structural approach its not the case.
let's use your method of reasoning in another matter, shall we?
q. what is the difference between cars and trains?
a. the basic difference is that cars have four wheels, a driver's seat with a steering wheel, a gear shift, and two or three pedals. while in the case of a train, that is not the case.
does that tell you what a train is?


anyway, your description is incorrect. the main difference between object-oriented programming and procedural programming is that in oop you put your code and the data used by that code in the same "place." in procedural programming, the code and data are not bound together as tightly as they are in oop.

however.

even that's not an accurate description, because in some languages, which are completely object oriented from the ground up, the code is still separate from the data. see this for a description of many things that are called "object-oriented".
soni
Naik
Posts: 70
Joined: Sat Oct 04, 2003 1:44 pm
Location: Karachi
Contact:

Hola

Post by soni »

Brothers donot waste your time in arguments, do the real thing, the OOP dance and chill..... :)
Please do not open M$Windoze.
Post Reply