C++ Private Structures - c++

I have read that the main differences between classes and structures (other than functions), is that class members default to private, whereas structure members default to public.
That implies that structure members can be private. My question is: Can you have private structure members? And if you can, what is the purpose of using private members? How would you even access them?

Yes structures can have private members, you just need to use the access specifier for the same.
struct Mystruct
{
private:
m_data;
};
Only difference between structure and class are:
access specifier defaults to private for class and public for struct
inheritance defaults to private for class and public for struct
How can you access them?
Just like you access private members of a class. i.e: they can only be accessed within the structures member functions and not in derived structure etc.

The only difference between struct and class is default access (with the exception of some weird template situations, see Alf's comments below). This means you can access private members in the same way as in a class:
struct foo {
int get_X() { return x; }
void set_X(int x_) { x = x_; }
private:
int x;
};
Whether you use struct or class, then, is purely a matter of style. I tend to use struct when all members are public (eg, if it's a functor class with no member variables and only public functions).

One thing that makes this useful is that you can also use the friend key word in structs, so private members can only be used and modified by those specific functions or classes or what not that you want to be able to modify it. This way the user can't modify those sections themselves. They won't even show up in the auto fill features, at least in visual studio.

Related

Why can this member function access a private vector

Now I have here a code snippet that I am trying to understand. I thought that only friend functions could access private member variables, so why is the method 'grad' able to access 'a' here to get its size?
#include <vector>
using namespace std;
class Polynom{
private:
vector<double> a;
public:
Polynom(const vector<double>& v): a(v) {}
int grad() { return a.size()-1; }
};
int main()
{
return 0;
}
Ask yourself, if private member access disqualified the rest of the class from access, what would be the point of a private field? You declare a potentially complex object, and then literally can do nothing with it? You have a private region that can interact with itself, but which cannot have any effect on any public facing functionality? You use the private structures as middlemen to write to public fields that you then use? None of these scenarios makes that much sense. Access modifiers protect the class from external influences, but the OOP model assumes the programmer will take care of themselves within the class.
It is noteworthy that there is one condition in which private members can not be accessed: Inheritance. The base class private variables are there in a derived class, but cannot be referenced directly. To be clear,these are the base class' private variables. The child class has its own private scope that it can declare within and access normally.

Access rights of undefined access member variable (neither public nor private)

What happens to a member variable in C++ if you don't define the access rights?
For instance, in the following code:
class Base {
int myQuestion;
public:
int myPublic;
private:
int myPrivate;
}
Who has access to myQuestion?
If you use the keyword class, access defaults to private. If you use struct it defaults to public.
That's pretty much the only difference between the two keywords.
The only other difference is when you inherit, class defaults to private inheritance, struct to public inheritance.
In a class, it is private. In a struct, it is public.
In class it is private, in struct it is public by default.

File scope and access rules

I saw something like the following in a IKM test, the code is in a single file:
class A{
public:
int a;
A();
protected:
int x;
private:
int y;
};
void ARandomFunction(){
//Implementation
}
which variables of class A can ARandomFunction() access? Generally speaking what can be accessed if the decalarations are all in the same file?
Those variables will be per-instance (non-static member variables), so you first need to create an object to access them. Only public members can be accessed from a free-standing function unless the function is declared friend of that class in which case all members can be accessed.
That said it doesn't matter if they are in the same file or not. Once the class definition is visible where the function is implemented the members can be accessed.
Being in the same file changes nothing, your function can only access a as it is public and your function is not a member of A (for the private members) nor a sub class of it (for the protected members).
To my knowledge, using your above example ARandomFUnction can access the public variables and functions regardless of inheritance. THe protected variables can only be accessed if ARandomFunction is contained in a class that inherits from or is a member of class A. Private variables and methods can only be accessed from the same class.

Private data types and member functions

What does it actually mean when you declare a variable or a member function as private in a C++ class? Besides the obvious fact that, these are accessible only by the member functions, How are they mapped differently on memory, than their public counterparts?
From standard docs, 9.2.12,
Nonstatic data members of a (non-union) class with the same access control (clause 11) are allocated so that later
members have higher addresses within a class object. The order of allocation of non-static data members with different access control is unspecified (11). Implementation alignment requirements might cause two adjacent members not to
be allocated immediately after each other; so might requirements for space for managing virtual functions (10.3) and
virtual base classes (10.1).
The standards has specs for the order of allocation of memory but there isn't much difference in the memory that is being allocated for a public data member and it's private counterpart..
Imagine that they were only acessible from member functions, then code like this would break:
class Foo {
int x; // this is private
public:
int& X() { return x; } // this is public, but returns a reference to a private variable
};
Foo foo;
foo.X() = 42; // look, I can set the value of a private member without being inside a member function
In short, one of the most common way to define get/setters in C++ would break if there was some sort of magic enforcing that a private variable must only be accessed by member functions. That's obviously no good.
public and private are there to help the programmer structure his code, and nothing more. They offer absolutely no security, and no runtime protection against code accessing a "private" member. Variables are only private in the source code. In the compiled code, there is no difference.
class priva
{
int x=10;
public:
int pub;
};
main()
{
priva a;
int *ptr;
ptr=&a.pub;
--ptr;//4 bite be4 there will be private data
cout<<"Private data having value of 10 is "<<endl;
cout<<*ptr;
}
Other than accesability there is no difference!
public and private are there only until compilation of the code. They don't have anything to do with the runtime or memory management.

Can functions be in a struct?

Can we describe functions in a struct? For example, is this code valid?
struct function {
int func() { return 5; }
};
Yes, the only differences between a struct and class in C++ are:
In C++, a structure is a class defined with the struct keyword. Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default. This is the only difference between structs and classes in C++.
Yes. There is no functional difference between a class and a struct, and anything you can do with one (such as defining a member function), you can do with the other.
If you're interested, the only difference is that members and base classes are private by default in a class, and public by default in a struct.
Yes, you can, the difference to class is the access limitations to its members and methods. The struct has all its members and methods public by default.
Yes, it is totally part of the standard.
A struct is a class with all its members public by default.