Is Object In Classes Different From The Structure Variable Of A Particular Structure Type In C++?
1 Answer - Sort by: Date | Rating
A structure in C++ is defined as 'a collection of simple variables'. It can have variables of different data types, and each variable (data item) within the structure is called MEMBER of the structure.
Example of a structure declaration would be as follows:
struct Distance
{
int feet;
float inches;
};
To use this structure a variable of Distance data type has to be defined like this:
Distance a;
And members can be accessed using the DOT OPERATOR like this:
a.feet = 5;
Similarly, if we look at the definition of a class, it is defined in much the same way. It also has data items, its variables are also defined in the same way, and data members are accessed with DOT OPERATOR.
class myClass
{
int d1;
float d2;
};
myClass a;
a.d1 = 5;
So, objects in classes (data items of a class) are NOT different from structure variables (data items or members of a structure). In fact in C++ structures and classes can be used interchangeably. But the way it is mostly taught is that structures can only hold data members(data items) while classes can have data members(data items) and well as member functions (functions defined or declared within a class).
Lastly, one important thing that a class has and structure doesn't is PRIVATE, PROTECTED AND PUBLIC specifies that protect data members and member functions from unnecessary access
Example of a structure declaration would be as follows:
struct Distance
{
int feet;
float inches;
};
To use this structure a variable of Distance data type has to be defined like this:
Distance a;
And members can be accessed using the DOT OPERATOR like this:
a.feet = 5;
Similarly, if we look at the definition of a class, it is defined in much the same way. It also has data items, its variables are also defined in the same way, and data members are accessed with DOT OPERATOR.
class myClass
{
int d1;
float d2;
};
myClass a;
a.d1 = 5;
So, objects in classes (data items of a class) are NOT different from structure variables (data items or members of a structure). In fact in C++ structures and classes can be used interchangeably. But the way it is mostly taught is that structures can only hold data members(data items) while classes can have data members(data items) and well as member functions (functions defined or declared within a class).
Lastly, one important thing that a class has and structure doesn't is PRIVATE, PROTECTED AND PUBLIC specifies that protect data members and member functions from unnecessary access
0
0
- Can Anyone Give Me The Code In C++ For Reversing A Linked Circular Queue Using Pointers?
- Write A Program In C To Find The Area Of Rectangle, Area Of Circle, Area Of Cube Using Overloading Function?
- State The Output That Will Be Produced By The Following C++ Program : #include #include Int Square (int Y ) { Return Y*y; } Void Main ( ) { For(int X=1;x?
- What Is The Input Electric On A Dsi?
- What Are The Classified Input In Economists?
- What Are The Classified Input Had Economics?
- What Is Input Technologies Trends?
- What Are The Input Technologies?
- What Floods Include Weatherwise?
- Why Are Economists Input As Timeless?
- What Is An Example Of An Input Devise?
- How To Make A Simple Programming?
- Where Can I Learn DirectX Or OpenGL In C++?
- What Does |= And &= Means In C++?
- What Is Input From Technology Means?
- Write A Program In C/C++ To Implement Cohen-Sutherland Line Clipping Algorithm. In This Implementation, Consider Two Cases Of A Line: Totally Visible, Totally Invisible, Against The Rectangular Clipping Window?
- Can Somebody Tell Me Why This Code Will Not Compile: #include "Stdafx.h" //added This Include #include //added Stream To Io Using Namespace Std; Int Main() { //declare Variables Int Choice=0; Std::cout?
- Sample Program Which Requires 2 Numbers, Input 1 And Input Which Will Be The Start Of A Fibonacci Series?
- Sample Program Which Requires 2 Input For Fibonacci Series?
- What Is Input Stream?
- How Do I Input Answers?
- How Do I Write A Program To Compute The Standard Deviation Of 5 Input Values. Create A Constant For The Number Of Values (5). Ask The User For The Numbers. Next, Compute The Mean (sum Divided By 5)?
- What Are Link Lists In C++?
- Where Can I Download C++ Programmes File?
- What Does Object Variable Or With Block Variable Mean?
- What Is An Object Reference Variable?
- What Is The Independent Variable Of Do Object Float In Saltwater Than Fresh Water?
- What Is Difference Between Structure Programming And Object Oriented Programming?
- Can You Define Classes Concept In Object-Oriented Programming?
- If The Net Force Of An Object Is Zero, What Type Of Force Does The Object Have?

New Comment - Comments are editable for 5 min.