English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I'm taking ANOTHER c++ course required for my degree in computer programming at my local community college, we are currently studying classes, I am not actually sure I understand the purpose of a class, since there ARE things like structs within the language.

Example, the definition of a struct is a collection of a fixed number of components in which the components are accessed by name, they MAY be of different types

Class- A collection of a fixed number of components. The components of a class are called members of the class.

This book even says that the ONLY difference between a struct and a class is the fact that by default all members of a struct are public, and all members of a class are private.

I do realize that within a class (not sure about struct) you are able to change the member access specifier...anyway...the question really is WHY are there classes if there are structs, am I just not getting the point yet?

2006-09-16 07:53:31 · 4 answers · asked by D 4 in Computers & Internet Programming & Design

4 answers

You need a class to be able to create new objects, a class encapsulates what the object is suppose to do / represent and should only expose methods for other users to access.

Anyone using your class does not have to worry about how it actually works internally, just how to use your methods that you designed within the class!

An explanation from the WIKI:

a class defines the abstract characteristics of a thing, including the thing's characteristics (its attributes or properties) and the things it can do (its behaviors or methods) or features). For example, the class 'Dogs' would consist of traits shared by all dogs, for example breed, fur color, and the ability to bark. Classes provide modularity and structure in an object-oriented computer program. A class should typically be recognizable to a non-programmer familiar with the problem domain, meaning that the characteristics of the class should make sense in context. Also, the code for a class should be relatively self-contained. Collectively, the properties and methods defined by a class are called members.

2006-09-16 08:06:45 · answer #1 · answered by Elmer Fudd 6 · 0 0

Although the class and struct are effectively identical, the struct is there for backwards compatibility with C. There's also the issue of defaulting to public access vs private when comparing the two; which you mentioned.

2006-09-16 08:10:37 · answer #2 · answered by jtrelfa 2 · 0 0

struct is a simple data structure. But class is similar to functions.

2006-09-16 08:03:59 · answer #3 · answered by iyiogrenci 6 · 0 0

http://www.aonaware.com/OOP1.htm

A tutorial.

2006-09-16 08:03:05 · answer #4 · answered by Anonymous · 0 0

fedest.com, questions and answers