Author Topic: C++, Constructors, Data Members And Member Functions -.-  (Read 2937 times)

0 Members and 1 Guest are viewing this topic.

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
C++, Constructors, Data Members And Member Functions -.-
« on: August 20, 2011, 06:51:55 pm »
Hi, I have a few questions.
I understand how to create basic classes, performing very basic tasks.
I know I should make data members private and member functions public.
I know how to use accesor and mutator methods to retrieve info or change the value of data members.
I kind of understand how to use a constructor.
What i dont understand is the concept behind these!
I do not understand why I should make data members private, so that they can only be accessed by member functions. Is this done for security???? Can someone explain that to me.
Also I do not understand why it is important to use contructors, as far as I know, the purpose of a constructor is to initialize data members inside a class.
But what if I want the value of the data members to be inputted by the user?
Do I still require a constructor?
Can someone explain that concept to me????
Thanks, sorry for teh long post :D

Offline gh0st

  • Sir
  • ***
  • Posts: 575
  • Cookies: 8
  • #DEDSec
    • View Profile
Re: C++, Constructors, Data Members And Member Functions -.-
« Reply #1 on: August 21, 2011, 07:23:34 pm »
Quote
I do not understand why I should make data members private, so that they can only be accessed by member functions. Is this done for security???? Can someone explain that to me.

to prevent to have all in disorder I mean to make it a source code more organizated if you think that its for security to prevent reverse engieneering I guess that there is where goes the part of "signatures" well at least in java is that I dont know if on C++ is the same
« Last Edit: August 21, 2011, 07:25:57 pm by gh0st »

xor

  • Guest
Re: C++, Constructors, Data Members And Member Functions -.-
« Reply #2 on: August 22, 2011, 11:02:56 am »
The reason why this is done is due to object orientation and encapsulation.

Lets take a car for example, (I'ma code in java, it's relatively similar, you'll hopefully get the idea).


The whole idea with encapsulation is that you don't need to understand everything the car contains, just as long as you've been given access to the methods you require to be able to successfully use it.

Code: [Select]
public class Car {
    private final double FUEL_CAPACITY = 55;
    private final double MAX_SPEED = 220;
    private double fuel = 0;
    private double speed = 0;
    private boolean engineStarted = false;

    public double getFuelLevel() {
        return this.fuel;
    }

    public void addFuel(double amount) {
        if (amount >= this.FUEL_CAPACITY)
            return;

        if (this.engineStarted == true)
            return;

        this.fuel += amount;
    }

    public void startEngine() {
        // Don't start the car without fuel, don't want to break it.

        if (this.fuel <= 0)
            return;

        this.engineStarted = true;
    }

    public void stopEngine() {
        this.engineStarted = false;


        while (this.speed >= 0)
            decelerate(5);


            // value of 5 is for example, normally you would calculate
            // vehicle weight, road friction, down force, etc.
    }


    public void accelerate(double kph) {

        if (this.engineStarted == false)
            return;


        if (this.speed + kph >= this.MAX_SPEED) {
            this.speed = this.MAX_SPEED;
        } else {
            this.speed += kph;
        }
    }


    public void decelerate(double kph) {
        if (this.speed - kph <= 0) {
            this.speed = 0;
        } else {
            this.speed -= kph;
        }
    }


    public void drive() {
        if (this.engineStarted == false)
            return;


        // example test run, drive to max speed then turn off the engine
        while (this.speed <= this.MAX_SPEED) {
            accelerate(10);
            // do some stuff to calculate fuel consumption
        }


        this.stopEngine();
    }
}

So as you can see, by removing direct access to the data members, and replacing the access with methods, you can more strictly control the conditions of access to the variables and also add in specified conditions.


Lets say that we had all of those data variables as public. Someone could create an instance of the car and say that the engine had more fuel than the actual tank can hold. In real world examples this can't happen, so you have to make sure it can't happen in the programming / virtual world as well. So to avoid programming bugs, errors and to more accurately imitate real world situations, we use getters, setters, methods and a combination of public / private variables.



I hope that cleared up a few things.
« Last Edit: August 22, 2011, 11:21:19 am by xor »

Offline Lionofgod

  • Knight
  • **
  • Posts: 164
  • Cookies: 6
    • View Profile
Re: C++, Constructors, Data Members And Member Functions -.-
« Reply #3 on: August 22, 2011, 06:49:40 pm »
Aright, Thanks a lot bro
That makes a lot of sense.
Ty +1 :D