How does a class vary from Structures ?
Hello my friends I am a student of a college and right now started to do a bit of programming , by now I have learned few basic things, I have learned a bit fundamental concepts about classes and objects, bit when I started to study about the structures it almost look similar to me, I will appreciate if anyone could reply to my post.
Re: How does a class vary from Structures ?
The structure also called as struct default access modifier kind is public. A struct must normally be used for grouping data. and the modifier of the class is always private, and the form for inheritance is private. A class must be used for grouping data and functions that work on that data. In short, the principle is to utilize struct when the intention is to cluster data, and utilize classes when we need data abstraction and, maybe inheritance.
Re: How does a class vary from Structures ?
Normally structures and classes are alike. In truth, you can utilize structures in almost precisely the same method that you utilize classes. The only proper variation between a class and a structure is that in a class the members are not visible to everyone but in a structure they are public ally accessible by default. I do not think that structure allows static members, but it is allowed by the class.
Re: How does a class vary from Structures ?
Here is an example of both class and structure, the basic form of structure is similar to a class, as the class us defined with a ‘class’ keyword , similarly a structure is created using a ‘struct’ keyword
class example
Code:
{
private:
int a;
public:
void function name()
{
// the code of a class
}
};
struct example
{
private:
int b;
public:
void funname()
{
// code for a structure
}
};
Re: How does a class vary from Structures ?
The above both code represents structures and classes each , this seems the most basic example the class is more widely used than a struct. Although in standard we can utilize a structure at every position whereas a class is used, in the majority of circumstances developers favor to make use of structures to cluster data, and classes to group both data and methods.