Thread: how to access "private or protected " in Class

  1. #1
    johnnypiere
    Guest

    how to access "private or protected " in Class

    hi,

    recently i have a big home work for final, and it has include all stuffes I have learn over a semeter ( my first c++ course).

    I write a complete working program, but my define classes like struct because the classes must have members in public, otherwise it won't work.

    However, homework requires that class members of the first class (Altitude) to be set as private or protected.

    can anyone help me to work around with this. I am very confuse with classes. The code below is very long. be kind to me and read all through.

    Thank



    **************************


    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <cmath>

    using namespace std;



    ofstream out; //global input/output variable (it is good in this case because
    ifstream in; //only function opening and print access to it)


    class Altitude //contains original datas of time and altitude
    {

    public:
    Altitude();

    double time[26]; //original datas from input file store in here

    double alti[26]; //

    };

    class Velocity: public Altitude //inherrited from class Altitude
    {
    public:
    Velocity();// constructor


    double velo[26]; //contains data points of velocity

    double alti_V[26]; // storage data points of altitude calculated intergrate of velocity

    };

    class Accelerationublic Altitude // inherritted from class Altitude
    {
    public:
    Acceleration(); //constructor

    double acce[26];

    double velo_A[26]; // storage data points of velocity calculated from acceleration

    };





    void openingfile(); //function opens input file and stores the datas to arrays

    void computeVelo(double time[] ,double alti[], double velo[]); // function calculates velocity

    void computeAcce(double time[], double alti[], double velo[], double acce[]); //function calculates acceleration

    void acce_Velo_Cal(double velo[], double acce[], double velo_A[], double alti_V[], double ti[]); //function intergrates acceleration
    //and intergrates velocity

    void print(double time[], double alti[], double acce[], double velo_A[], double velo[], double alti_V[]);


    void main()
    {




    int i = 0;


    Altitude ti;


    Velocity ve;


    Acceleration ac;


    openingfile(); //function open input file

    while(!in.eof())

    {
    in>>ti.time[i]>>ti.alti[i];

    i = i+1;

    }

    //functions call

    computeVelo(ti.time , ti.alti, ve.velo);

    computeAcce(ti.time, ti.alti, ve.velo, ac.acce);

    acce_Velo_Cal(ve.velo, ac.acce, ac.velo_A, ve.alti_V, ti.time);

    print(ti.time, ti.alti, ac.acce, ac.velo_A, ve.velo, ve.alti_V);



    in.close();
    out.close();


    return;
    }


    //function open the input file

    void openingfile()
    {
    char *fname; // a dynamic pointer
    fname = new char[20]; //allocated memory when exe

    cout<<"******************************************* *************************************"<<endl;
    cout<<"************************ ENGINEERING 50 FINAL PROJECT***************************\n\n"<<endl;

    cout<<"This program will read a data table with two columes of time Vs altitude "
    <<"from a file. It would prompt user to provide the input and output"
    <<"file names. After that the program will calculate the acceleration,"
    <<"and velocity by numerical method. Thus, it regenerates from calculated "
    <<"acceleration to get velocity and from velocity to get altitude. As a "
    <<"result,the output file contain final datas that let engineers to interpret"
    <<"the precision or accuracy between forward and backward programing. Last,"
    <<"The comparision will be plotted using Excel and Matlab software.\n"<<endl;

    cout<<"\nPlease enter the input file name \n(need including the file extention): ";
    cin>>fname;
    in.open(fname);
    if (in.fail()) //check the input process
    {
    cout<<"\nInput file opening failed.\n";

    exit(1);
    }

    delete []fname; //free the memory

    return;



    }

    //function print out the results of calculation

    void print (double time[], double alti[], double acce[], double velo_A[], double velo[], double alti_V[])

    {
    char *gname; //another pointer
    gname = new char[20]; //and memory allocated

    out<<fixed<<showpoint<<setprecision(2);
    cout<<"\nPlease enter the output file name, \n(need including the file extention): ";
    cin>>gname;

    out.open(gname);
    if (out.fail())
    {
    cout<<"\nOutput file opening failed.\n";
    exit(1);
    }


    out<<setw(7)<<"Time (s)"<<setw(13)<<"Alti (ft)"<<setw(15)<<"Velo (ft/sec)"
    <<setw(17)<<"Acce (ft/sec)"<<setw(20)<<"Intgr_A (ft/sec)"<<setw(20)<<"Intgr_V (ft/sec)"<<endl;
    out<<"******************************************** ***********************************************"<< endl;
    out<<"\n"<<endl;

    for (int i = 0; i<26;i++)
    {



    out<<setw(6)<<time[i]<<setw(14)<<alti[i]<<setw(14)<<velo[i]<<setw(14)<<acce[i]
    <<setw(20)<<velo_A[i]<<setw(20)<<alti_V[i]<<endl;
    }
    delete []gname;
    return;


    }


    Altitude::Altitude() //intialize all array adresses to zero
    {
    double time={0.0};
    double alti={0.0};
    }



    Velocity::Velocity()
    {
    double velo={0.0};
    double alti_V={0.0};
    }

    Acceleration::Acceleration()
    {
    double acce = {0.0};
    double velo_A = {0.0};
    }



    void computeVelo(double time[26] , double alti[26], double velo[26])
    {

    double *delta;
    delta = new double;


    velo[0]=0.0; //initial velocity is zero


    for(int i=1;i<=24;i++)
    {

    *delta = (time[i] - time[i-1]);

    velo[i] = (alti[i+1] - alti[i-1])/(2*(*delta)); //the rest of data point using formula
    // ( x(i+1) - x(i-1)) / (2 * delta x)

    }

    velo[25] = (alti[25]-alti[25-1])/(*delta); //last data point using formular : (xi - x(i-1))/delta x

    cout<<"Velocity is calculated!"<<endl;

    delete delta;

    return ;

    }


    //Function compute acceleration
    void computeAcce(double ti[],double al[],double velo[],double acce[])
    {


    double *delta;
    delta = new double;

    acce[0]=0.0; //initial acceleration = 0


    for(int i=1;i<=24;i++)

    {
    *delta = (ti[i] - ti[i-1]); //delta x = x2-x1

    acce[i]=(al[i-1]-(2*al[i])+al[i+1])/(pow(*delta,2)); //acceleration = ( Y(i-1)-2*Y(i)
    // + Y(i+1) )/(square of delta x)

    }

    acce[25]= (velo[i]-velo[i-1])/(10); //constant delta = 10 use for last point


    cout<<"Acceleration is calculated!"<<endl;
    delete delta;
    return ;
    }




    void acce_Velo_Cal(double velo[], double acce[], double velo_A[], double alti_V[], double ti[])
    {
    int j=0 ; //since delta is constant and = 10 in this case
    double *delta;
    delta = new double;

    velo_A[0] = 0.0;

    for(int i=1;i<26;i++)
    {
    *delta = ti[i]-ti[i-1];

    velo_A[i]= velo_A[i-1] + *delta*acce[i]; //sum up of area before and it's value to get
    // point by point velocity.
    }

    cout<<"Intergration of acceleration is calculated!"<<endl;

    alti_V[0] = 60.0;

    for (j = 1; j<26; j++)

    {
    *delta = ti[j]-ti[j-1];
    alti_V[j]= alti_V[j-1] + velo[j]*(*delta); //sum of area before and its value to get point
    // by point altitude.

    }
    cout<<"Intergration of velocity is calculated!"<<endl;
    delete delta;

    return ;
    }

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    sorry, I post the previous code above in hurry without read the "sticky" how to post a code.

    I couldn't edit because I posted without login as my nick now.!!

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    That is a lot of code to not have code tags But I accept your explanation.

    If class A inherits class B, B can access protected member of A. However, only A can access its private members directly. You can use member functions to act as an intermediary so that you can read or write (possibly both) a private member. You can also make B a friend of A and then B can access A's private members. Just remember that private members are made private for a reason. There is no point to doing something like this:

    Code:
    class A {
    public:
        int &get_private_member() { return x; }
    private:
        int x;
    };
    Also, if you're saying that it is illegal to compile a class that has no public members then you are wrong. Compilers vary on this subject, but you can usually disable this sort of thing.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    6
    thanks for reply,

    in my book, talks the same as you do, but I can not really image how it will work. That's why I need help to work around, please give me example how to fix my program.

    My book does not cover very well in this section classes, pointers..

    correct: the above code is working fine. Just a required of hw that the first class has to be set private.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. how do u access a class protected area?
    By CwannaB in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 08:52 AM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM
  5. class access
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 01-24-2002, 08:00 AM