473,386 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Class and Structure

what is the difference between a class and a structure(struct)...when to use class and when to use struct???
Mar 16 '14 #1

✓ answered by weaknessforcats

There is no difference between a class and a struct except one:

The default access to a class member is private whereas the default access to a struct member is public.

Structs are in C++ because they are inherited from the parent language C. A struct is the only way to group member variables under a single name.

A class is an object oriented program concept. A class groups member variables under a single name.

The big secret in C++ is that classes are implemented as structs. No kidding. Create a C++ class using public and private members, add constructors and a destructor, maybe add operator overloads. When it compiles without error, just change the keyword "class" to "struct" and recompile. There will be no errors.

You use class in C++ all the time. You use struct in C (you have no choice). You use struct in C++ to advertise that you really don't understand this object business.

2 3543
weaknessforcats
9,208 Expert Mod 8TB
There is no difference between a class and a struct except one:

The default access to a class member is private whereas the default access to a struct member is public.

Structs are in C++ because they are inherited from the parent language C. A struct is the only way to group member variables under a single name.

A class is an object oriented program concept. A class groups member variables under a single name.

The big secret in C++ is that classes are implemented as structs. No kidding. Create a C++ class using public and private members, add constructors and a destructor, maybe add operator overloads. When it compiles without error, just change the keyword "class" to "struct" and recompile. There will be no errors.

You use class in C++ all the time. You use struct in C (you have no choice). You use struct in C++ to advertise that you really don't understand this object business.
Mar 16 '14 #2
Sherin
77 64KB
Class

In OOP, a class is a blueprint from which individual instances are created.
A class is a blueprint for creating objects (a particular data structure), providing initial values for state (member variables or attributes), and implementations of behavior (member functions or methods).
We define a class using the class keyword.
Classes are of reference types.
All the reference types are allocated on heap memory.

Example

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2.  
  3. class Test { 
  4.     int x; // x is private 
  5. }; 
  6. int main() 
  7. Test t; 
  8. t.x = 20; // compiler error because x is private 
  9. getchar(); 
  10. return 0; 
Structure

A struct is similar to a class in terms of definition and instance creation.
We define a struct using the struct keyword.
Structs are of value types.
All the value types are allocated on stack memory.
Classes can contain constructor or destructor.
Structure does not contain parameter less constructor or destructor, but can contain Parameterized constructor or static constructor.

Example

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2.  
  3. struct Test { 
  4.     int x; // x is public 
  5. }; 
  6. int main() 
  7. Test t; 
  8. t.x = 20; // works fine because x is public 
  9. getchar(); 
  10. return 0; 
  11.  
Aug 20 '20 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Victor Hannak | last post by:
I have two classes derived from a base class. The two derived classes each utilize a structure that is slightly different from one another. i.e. DerivedClass1: struct NodeStruct { float...
1
by: Randi | last post by:
Hi, Looking for some help with this payrool project I have for class. This is what the instructor asks for so far. I have it working without errors but am getting some funky numbers. I am not...
1
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
1
by: Rob Andrew | last post by:
Hi All, I'm new to VS.net, coming from Delphi, and there is something that I'm trying to clarify in my head about VS.net. Suppose you had a class Employee (Name, Age, Title, etc). Lets...
3
by: Tony Young | last post by:
Hi, I am very interested to know which one of the two functions below is used more often by people. The 1st way intends to let B prepare the data and A perform the action. The 2nd way intends...
3
by: Jim Langston | last post by:
I am attempting to map the variables in a class or structure to use with MySQL. I got something to work but I'm not happy with it. Here is a snippet showing what I'm not happy with: class...
7
by: SteveT | last post by:
Can someone point me in the right direction? Somewhere I read that you reference a strongly typed dataset as if it were a class structure. For example, <SomeTests> <TestsGroups> <Group>...
6
by: olympus_mons | last post by:
Hi, I generated C# classes from some complex XMLSchemas usind xsd.exe. The result is that I get a class hierarchy that is quite deep (well for me 8 levels are deep). What I'm curiuos about is,...
2
by: K Viltersten | last post by:
Suppose there is the following class structure. class S {...} class A : S {...} class B : S {...} class A1 : A {void m(){...}} class B1 : B {void m(){...}} class B2 : B {...} Just to clarify...
3
by: shapper | last post by:
Hello, I have 3 type of h2 headers on my web site: 1 - Post titles on a blog (Ex: <h2>New documents available for download</h2>) 2 - Content section title (Ex: <h2>Contacts</h2>) 3 -...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.