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

Home Posts Topics Members FAQ

Class and Structure

16 New Member
what is the difference between a class and a structure(struc t)...when to use class and when to use struct???
Mar 16 '14 #1
2 3549
weaknessforcats
9,208 Recognized Expert Moderator Expert
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 New Member
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
2081
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 NodeValue; ListStruct *NextNode; }
1
8654
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 sure if they would be correct anyways. Below is the instructions. and below that is what I have. Any tips or hints would be highly appreciated. ...
1
3254
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 structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then...
1
2112
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 further assume that you are creating a web service, with a .net windows application as a client to that web service. Given that you wish to share the...
3
1717
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 to let A initialize data in a general way and B specialize the data. My question may be dumb because it depends on needs. But I am preparing a...
3
1222
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 COffsetMap { public: COffsetMap() {} virtual ~COffsetMap() {} void SetBase( void* Base ) { Base_ = reinterpret_cast<char*>( Base ); }
7
2146
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> <TestName>TestA</TestNmae> <Test>Run1</Test> <Test>Run2</Test>
6
2569
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, that if I create an instance of my top level element I still need to create instances of all sub-elements. What would be the best way to do some sort...
2
1500
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 the issue. A1 and B1 share a method (void m() ) AND B2 doesn't have that
3
1792
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 - Sidebar content section title (Ex: <h2>Publicity</h2>)
0
7852
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
8216
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8349
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
7974
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
6629
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
5719
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
5395
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2364
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
1455
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.