473,587 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why we need classes

I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??

Jul 23 '05 #1
14 3779
structures are public by default and classes are private by default.
Unless you use the keywords private, public or protected, any member
funtions or member data in a structure will be public. The idea behind
encapsulation is to hide the details, it is desireable to make private
data and provide public interface methods that the user of the class
can use.

Jul 23 '05 #2
On 27 Feb 2005 19:34:41 -0800, "Pratts" <it*********@re diffmail.com>
wrote in comp.lang.c++:
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??


A better way to ask this question would be:

"Why do we need structs in C++ when we have classes?"

There is a difference in default access, but this is really trivial.

Structs are needed for backwards compatibility with C code, of which
there were millions of lines in existence as C++ came into being.
Classes are little more than a documentation mechanism.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 23 '05 #3
"Pratts" writes:
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??


If you mean why do we need the keyword "class" in addition to stuct, the
answer is, we don't. It's just for convenience and, as typically used,
improves documentation.
Jul 23 '05 #4

"Pratts" <it*********@re diffmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??

structures do not provide anywhere near the functionality you get from
classess.

Classess have constructors, that can be modified./overloaded.
Classes have destructors, that can be modified/ overloaded.
Classes allow for operator Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.

The differences are vast. You will have to write some code to begin to
understand the impact.

I struggled with it for several months.

You gain Power, Robustness. A level of Protection, while increasing the
longevity of your code.

I can send you some examples done with both structures and class object, if
you'd like to see the difference.

dan
DH************* @cox.net


Jul 23 '05 #5
> Classess have constructors, that can be modified./overloaded.
Classes have destructors, that can be modified/ overloaded.
Classes allow for operator Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.


struct was upgraded in C++ and can do all of that too. Try it.

I personally stopped using the class keyword and went back to struct.
The reason is I can eliminate that silly "public:" statement at the
beginning needed to expose the constructor. In the end, "class" proved
to be just an unnecessary annoyance with no value add for me. I do use
public, protected, and private extensively to define structs.

Jul 23 '05 #6
>> Classess have constructors, that can be
modified./overloaded. Classes have destructors, that can
be modified/ overloaded. Classes allow for operator
Overloads.
Classes have member functions, static functions.
Public, Private, and Protected Data and code.
Classes incapsulate. Structures Arrange.


struct was upgraded in C++ and can do all of that too.
Try it.

I personally stopped using the class keyword and went
back to struct. The reason is I can eliminate that silly
"public:" statement at the beginning needed to expose the
constructor. In the end, "class" proved to be just an
unnecessary annoyance with no value add for me. I do use
public, protected, and private extensively to define
structs.

Indeed. I think that 'class' and 'struct' where meant to be equivalent. I never
touched another class after reading what Bjarne Stroustrup wrote about it in the
Annotated C++ Reference Manual, Addison-Wesly Publishing Company, 1995,
Paragraph 11.2:

<quote>
(...) For example, novices often don't know about access specifiers and get
confused by this:

class X { public: f(); };
class Y : X { }; // no access specifier
// private by default

void g(Y* p)
{
p->f(); // error
}

Even experts can get caught. A compiler can be most helpful by issuing a warning
for the missing access specifier.
Having private as the default was chosen to reflect the general view that
things that are not explicitly declared public are private. Defining a default
access specifier was probably a mistake.
</quote>

He says that the default access specifier 'private' for classes was a mistake.
There should have been _no_ default access specifier, ie class should be
'public' by default, just like struct is. The difference between class and
struct is based on a mistake. A class was meant to act just like a struct does.

Jul 23 '05 #7

"Pratts" <it*********@re diffmail.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .
I am a new one who have joined u plz try to help me bcoz i could not
find ny sutiable answer foer this Question
Qus>>why do we need classes when structures provide similar
functionality??

we r n nglsh lang nwsgrp her, not IM
try complt wrds nxt tm

Jul 23 '05 #8
buddy very sorry to say that
u must go through the things again bcoz structures can do all that
constructor
destructors
operator Overloads and every thing that class can do
plz try this out
byEEEEEEEEEEE

Jul 23 '05 #9

"Pratts" <it*********@re diffmail.com> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
buddy very sorry to say that
u must go through the things again bcoz structures can do all that
constructor
destructors
operator Overloads and every thing that class can do
plz try this out
byEEEEEEEEEEE


Can structures have pure virtual Functions?
Can a structure prevent a programmer from erroneously ovewriting data
elements?
Can a structure have Private data?
Can a structure have Protected data?

The truth is I'm not sure, I use structs to set up Common data that the
Class objects will be using, and sharing with the outside world.
Jul 23 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1615
by: Robert | last post by:
I have two tables (classes and students). Right now, I'm generating a report in asp that shows a list of classes, the enrollment for each class, and how many seats are available (see query below). SELECT classes.classid, classes.classname, classes.maxstudents, classes.Active COUNT(students.class) AS countstudentsclass, classes.maxstudents...
2
1433
by: cm500 | last post by:
I'm very new to databases so bear with me. What I need is a way to track the training for the employees at my firm. I have 40 classes that I will be teaching on various subjects and various levels of difficulty. I have 100 employees that will need to take some of the classes offered (already determined who needs to take what). I need a...
5
3479
by: Benne Smith | last post by:
Hi, I have three enviroments; a development, a testing and a production enviroment. I'm making a big application (.exe), which uses alot of different webservices. I don't use the webservices by adding a WebReference, since it does not allow me to keep state (cookiecontainer) or to specify functions on the classes (like if i want to...
2
1981
by: Andrew S. Giles | last post by:
OK, Ive run my head into this wall for too long. I need help. I am developing an applicaiton in C# to present a user with a GUI to specify a configurable list of machines that he wants to listen to the output of. Specify a filename to shove all of the data (into Excel), and start the whole thing going. I get that done no problem. The...
8
2315
by: Tim Geiges | last post by:
Since I am being challenged with learning c# I figured I could pass some of the pain on to you guys :-) I have another question(this one is important for me to fix before I can get my app to Beta) My app (an image viewer) opens with a Main form with a file explorer if you open the program with the exe, but opens with the ImageView form if...
1
1158
by: bonk | last post by:
I have a set of unmanaged c++ classes that internally need to use managed classes (WPF formerly know as "Avalon") but I do not want to compile the whole MFC project with the /clr switch. What options do I have in that case? Could I a) swap out all the classes that need to use managed classes into its own dll and compile ONLY that one with the...
10
2014
by: Luke Meyers | last post by:
So, just a little while ago I had this flash of insight. It occurred to me that, while of course in general there are very good reasons for the conventional two-file header/implementation separation for each C++ class, there are cases in which this paradigm contributes nothing and simply introduces extra boilerplate overhead. The...
18
2318
by: bsruth | last post by:
I tried for an hour to find some reference to concrete information on why this particular inheritance implementation is a bad idea, but couldn't. So I'm sorry if this has been answered before. Here's the scenario: We have a base class with all virtual functions. We'll call this the Animal class. We then make two classes Fish and Bird...
1
3119
by: Tyno Gendo | last post by:
Hi everyone I need to move on a step in my PHP... I know what classes are, both in PHP4 and 5 and I'm aware of "patterns" existing, but what I'm looking for are some real world projects eg. Open Source that people consider to use classes and patterns correctly. I lack a senior person to lead me in this so I feel I'm losing out on only...
3
2006
by: =?Utf-8?B?SmF5IFZpbnRvbg==?= | last post by:
I see general messages about how to learn .NET but I have an immediate requirement to ramp up my old skills very quickly. Can anyone recommend the FASTEST way for me to get almost-competent in VB.NET (C# will come later)? This must be an online or book adventure because I can't totally disappear to attend physical classes. I have 15 years...
0
7920
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7849
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8347
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7973
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6626
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5718
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3844
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2358
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1454
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.