Accessing private data members

This is a discussion on Accessing private data members within the C++ Programming forums, part of the General Programming Boards category; I need to know how to take private data members from one class and use them to access private data ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    43

    Accessing private data members

    I need to know how to take private data members from one class and use them to access private data members of another class. I have a class like this:

    Code:
    class One
    {
    	// Other code
    
    	private:
    
    	Two *TC[3] // Reads in teaching schedule from other class
    
    }
    
    class Two
    {
    
    	// Other code
    
    	private:
    
    	string courseDepartment;
    	string courseNumber;
    	string numStudents;
    }
    In the first class it holds instructor info like name and stuff and i need something in class one that will allow me to access the teaching schedule that is stored in class two, each instructor has three classes they will teach. I hope this makes sense.

  2. #2
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297

    Re: Accessing private data members

    Originally posted by maloy
    i need something in class one that will allow me to access the teaching schedule that is stored in class two, each instructor has three classes they will teach. I hope this makes sense.
    Code:
    class One
    {
    	// Other code
    
    	private:
    
    	Two *TC[3] // Reads in teaching schedule from other class
    
    }
    
    class Two
    {
    
    	// Other code
    
    	private:
                    
                    friend class One;
    
    	string courseDepartment;
    	string courseNumber;
    	string numStudents;
    }
    consider it done.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  3. #3
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    "friend" can be used to naughty purposes but assuming you want to use it here, class One is now allowed to do stuff with class Two private data
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    thanx

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    is there a way i can have a print function in one class be able to be called from a member function of another class?

    Code:
    public:
    	Instructor();   // constructor
    	Instructor(string fn, string mi, string ln, string eid);
    	~Instructor();
    
    	friend class Course;
    	string getFirstName() const;
    	string getLastName() const;
    	string getMiddleInitial() const;
    	string getEmployeeID() const;
    
    
    	void setFirstName(string fn);
    	void setLastName(string ln);
    	void setMiddleInitial(string mi);
    	void setEmployeeID(string sid);
    //This one->	void printInstructor(string firstName, string lastName,
    					   string middleInitial, string employeeID);
    I am trying to call that print function from this member function in another class

    Code:
    Course::Course(string department, string number,string students)
    {
    	courseDepartment = department;
    	courseNumber = number;
    	numStudents = students;
    	Course P;
    	
    	P.printInstructor("joe", "j", "smith", "7654");
    }

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    it's public. why wouldn't you be able to? am I missing something?
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    it gives me an error when i leave it like that
    error C2039: 'printInstructor' : is not a member of 'Course'

  8. #8
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    wait wait wait.... you're trying to trick me!!!

    that first class is not "Course" is it? You declared an instance of "Course" inside the constructor for "Course". Don't you see a problem with that?

    If the first class is not Course then that function obviously isn't a member of Course...... of course.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    umm no course is not the first class but its not working see this is what i have

    Class Instructor is the first class and it has a function that is public called printInstructor that takes 4 strings as parameters and then after that i have a class called Course and one of the member functions for course looks like this:

    Code:
    Course::Course(string department, string number,string students, Instructor *S[])
    {
    	courseDepartment = department;
    	courseNumber = number;
    	numStudents = students;
    
    	//right in here is where i want to call the function printInstrcutor
    	
    	printCourse(courseDepartment, courseNumber, numStudents);
    	
    }

  10. #10
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    Instructor instructor;

    instructor.printInstructor(courseDepartment, courseNumber, numStudents);


    just call it Print though. you know its an instructor. Methods are "verbs", objects are "nouns" in a sense.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    43
    having a problem with control in my program i guess. this code im posting is supposed to read data from this file and then do some things . the first function below is the first function called and then i just put in the other functions that the first function uses below.

    Code:
    void readCourse(ifstream &I, Instructor *S[], Course *C[] )
    {
    
    	string courseDepartment;
    	string numStudents;
    	string courseNumber;
    	string employeeID;
    	int courseIndex=0;
    	
    
      while(!I.eof())
       {
        I >> employeeID >> courseDepartment >> courseNumber >> numStudents;
    
        if(I.eof())
         break;
    
        for(int instructorIndex = LOOP_BASE;
                instructorIndex < MAX_INSTRUCTORS;
                instructorIndex++)
         {
          if(S[instructorIndex]->getEmployeeID() == employeeID)  
           {
             
    				C[courseIndex]= new Course(courseDepartment,
    					courseNumber, numStudents, S, C);
    				printData(S);
    			
    		}
    	  }
    	}
      
    }
    Code:
    Course::Course(string department, string number,string students, Instructor *S[], Course *C[])
    {
    	courseDepartment = department;
    	courseNumber = number;
    	numStudents = students;
    
    	
    	
    	//printCourse(S, C);
    	
    }
    Code:
    void printInstructor(Instructor *S)
     {
    
    	cout << S->getFirstName();
    	printSpaces(FIRST_NAME_FIELD_WIDTH - S->getFirstName().length());
      
    
    	cout << S->getLastName();
    	printSpaces(LAST_NAME_FIELD_WIDTH - S->getLastName().length());
    
      cout << ' ' << S->getMiddleInitial()
           << ' ' << S->getEmployeeID()<<" has been hired at XYZ"<<endl;
      
    
     }
    void printData(Instructor *S[])
     {
      for(int index = LOOP_BASE; index < MAX_INSTRUCTORS; index++)
       {
        if(S[index] != NULL_POINTER)
    		printInstructor(S[index]);
    		break;
       }
     }

    But when it reads from this input file:

    8300 CSE 1325 92
    3942 MATH 2325 18
    3942 MATH 2326 25
    4301 HIST 1311 26
    8300 CSE 1320 24

    it keeps repeating this:

    This is line number 250 of the file C:\Documents and Settings\Massa\Driver.c
    pp which was
    compiled on Oct 4 2002 at time 11:46:46.
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Ray L. Jones 3942 has been hired at XYZ
    Press any key to continue

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Idea 1:
    try commenting out the break; line

    Idea 2:
    add an internal check like:

    cout << index;
    cin.gets();

    before the call to printInstructor() in printData().


    Idea 3:
    Also how do you call printData()? I suspect the the line being printed is the for the first Instructor * in S and that you cycle through an array or a file calling printData() each time through which causes just the information for the first Instructor * to be printed out over and over and over.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. still confused with constructors and private data members
    By freddyvorhees in forum C++ Programming
    Replies: 2
    Last Post: 07-26-2008, 10:29 AM
  2. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 01:34 PM
  3. Using private class members in static functions
    By sethjackson in forum C++ Programming
    Replies: 2
    Last Post: 09-23-2005, 10:54 AM
  4. C diamonds and perls :)
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-16-2003, 11:19 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21