Bloomberg LP Interview Question
Software EngineersCountry: United States
Interview Type: Phone Interview
Public and private are both access specifiers in C++.
When a class is created or declared by default the specifier is private, to change the specifier to public one has to declare it with the public keyword.
The private members are private to the class or the object created from the class.
Those can't be accessed from the outside world, where the public functions are the interface to the outside world. Outside world can play with the data members using the public member functions .
//
// In C++, what's the difference between public and private?
// what's the purpose of this and please illustrate a design
// example with this.
//
// -- ycw March 07, 2015 in United States
// Bloomberg LP Interview Question
//
// The whole purpose of private, protected, friend and public in
// a C++ program is to reduce code management complexities that
// are often associated with large software projects. Typically,
// the more code you write, the more complex the code becomes.
// One method developed to act as a counter-measure to coding
// complexity which grows exponentially with each modification
// and addition to the code base is to hide as many details in
// a given module or component of software from all other given
// modules or components of software as possible.
//
// In C++ we have the ability to hide how a module gets things done
// internally from how the the module can be used externally.
// This is known as loose-coupling as oppose to tight-coupling with
// the idea of giving the developer better ways of upgrading code
// or fixing code without having to worry about issues in other
// parts of the software base. In effect, more cleaner your public
// interface and the more seperate the public interface is from
// it's private behavior, the more the same or another developer
// can make changes or correct code behavior over time.
//
// The hiding details of a class's implementation is called
// Encapsulation.
//
//
// (c) 2016 Perry Anderson, All Rights Reserved, worldwide.
//
//
/**
* In it's simplest incarnation you would hide as many
* details of the class inside and private away from
* it's public interface. Here we make int steps a private
* member, the only way to increase the amount of steps
* a person takes it to call it's public method walk().
*
* Trying to access the private member from outside the
* class will result in a C++ compiler error. The program
* won't even compile as the compiler is trying to tell you
* that you trying to access a member marked as private.
*
*/
class Person {
private:
int steps;
public:
Person() : steps(0)
{
}
void walk() {
steps++;
}
};
int main() {
Person instance;
// compiles, no problem and the number of steps
// are increased by one
instance.walk();
// this will not compile as the class specification
// has marked the member int steps as private and not
// publically available
instance.steps = instance.steps + 1;
}
**
* The first version of the class only needed to update the
* steps member by one. In a theorectial future version of the
* class perhaps things are much more complicated. The following
* is a graphic example of how complicated things can get for
* the purpose of illustration. But it shows the benefits of
* making things private as the changes made to the new
* version of the class have no effect on how the class
* is used via it's public interface.
*
*/
class Person {
private:
int steps;
void howWeReallyWalk() {
int a = 123, b (456), sum = a + b;
cout << "The sum of " << a << " and " << b << " is " << sum << endl;
int x = 3, y = 5;
cout << a << '+' << b << '=' << (a+b);
cout << "vector demo" << endl;
vector<int> left(7);
vector<int> right(7);
left[5] = 7;
right[5] = 8;
cout << left[5] << endl;
cout << right[5] << endl;
vector<int> biggest(
pick_vector_with_biggest_fifth_element( left, right )
);
cout << biggest[5] << endl;
steps = a + b + c;
}
public:
Person() : steps(0)
{
}
void walk() {
howWeReallyWalk();
}
};
int main() {
Person instance;
// compiles, no problem and the number of steps
// are increased by one
instance.walk();
}
Public Vs Private:
- Bhanuprasad March 21, 2015Public:members of a base class can be accessed by members of that base class, members of its derived class as well as the members which are outside the base class and derived class.
Private:members of a base class can only be accessed by members of that base class .
Eg:
#include <iostream>
using namespace std;
class Box
{
public:
// Constructor definition
Box(double l=2.0, double b=2.0, double h=2.0)
{
cout <<"Constructor called." << endl;
length = l;
breadth = b;
height = h;
}
double Volume()
{
return length * breadth * height;
}
int compare(Box box)
{
return this->Volume() > box.Volume();
}
private:
double length; // Length of a box
double breadth; // Breadth of a box
double height; // Height of a box
};
int main(void)
{
Box Box1(3.3, 1.2, 1.5); // Declare box1
Box Box2(8.5, 6.0, 2.0); // Declare box2
if(Box1.compare(Box2))
{
cout << "Box2 is smaller than Box1" <<endl;
}
else
{
cout << "Box2 is equal to or larger than Box1" <<endl;
}
return 0;
}
}