Class structure (Newbie)

S

sam

Hi,
See this example of class:-
class a{
private:
int o,t;
public:
a( ) { };
a(int s,int g)
{o=s;
t=g;
}
};
My question is see o=s; and t=g;
What we are doing this we are inserting the public value to private
value or converting public to private.I new to c++ programming but I
want to understand the logic behind this or the technique behind
this(Simply I curious about how this work? )
Thanks for advance.
 
S

Scott McPhillips [MVP]

sam said:
Hi,
See this example of class:-
class a{
private:
int o,t;
public:
a( ) { };
a(int s,int g)
{o=s;
t=g;
}
};
My question is see o=s; and t=g;
What we are doing this we are inserting the public value to private
value or converting public to private.I new to c++ programming but I
want to understand the logic behind this or the technique behind
this(Simply I curious about how this work? )
Thanks for advance.

'public' and 'private' refer to class members. s and g are not class
members, they are input parameters from the world outside the class.
 
N

Noah Roberts

sam said:
Hi,
See this example of class:-
class a{
private:
int o,t;
public:
a( ) { };
a(int s,int g)
{o=s;
t=g;
}
};
My question is see o=s; and t=g;
What we are doing this we are inserting the public value to private
value or converting public to private.I new to c++ programming but I
want to understand the logic behind this or the technique behind
this(Simply I curious about how this work? )

The compiler simply refuses to compile anything that accesses those
variables directly.
 
A

amit

sam said:
Hi,
See this example of class:-
class a{
private:
int o,t;
public:
a( ) { };
a(int s,int g)
{o=s;
t=g;
}
};
My question is see o=s; and t=g;
What we are doing this we are inserting the public value to private
value or converting public to private.I new to c++ programming but I
want to understand the logic behind this or the technique behind
this(Simply I curious about how this work? )
Thanks for advance.

You are not 'inserting' but copying value of s and g into o and t
respectively. o and t will continue to remain private.
 
J

Jim Langston

sam said:
Hi,
See this example of class:-
class a{
private:
int o,t;
public:
a( ) { };
a(int s,int g)
{o=s;
t=g;
}
};
My question is see o=s; and t=g;
What we are doing this we are inserting the public value to private
value or converting public to private.I new to c++ programming but I
want to understand the logic behind this or the technique behind
this(Simply I curious about how this work? )
Thanks for advance.

a(int s, intg) is a constructor for your class a. It takes two interger
paramters passed by value, then assigns these values to the class's private
variables o and t. The class itself can use it's own private variables in
it's own methods (including constructor).

Incidently, the same thing can be achieved by:

a( const int s, const int g): o(s), t(g) {};

which basically does the same thing except it initializes o and t to the
values instead of assigns them.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top