Home › Forums › C Programming › How to access member of a class from a diff class
- This topic has 0 replies, 1 voice, and was last updated 16 years, 6 months ago by cheeranjiv.
Viewing 0 reply threads
- AuthorPosts
- March 14, 2008 at 6:29 am #2077cheeranjivParticipant
Hi ,
I am a beginner in C++.
I want to know how can we access data member of a class from another class. I am facing problem in visualizing the things in writing separate .h and .cpp file.
Here I explain the things.We have three classes.
In a.cpp (assuming the a.h files is wriiten )12345678910#include "a.h"<br />A::A()<br />{<br />int ii=1;<br />}<br />void A:: fun_a()<br />{<br />cout<<"Value in A:"<<ii<<endl;<br />}<br />In b.h
12345678910111213#include "a.h"<br />Class B<br />{<br /><br />int jj;<br />A b_aa;<br />public:<br /><br />void fun_b();<br />void change_ii();<br /><br />};<br />In b.cpp
1234567891011121314151617181920#include "b.h"<br />B::B()<br />{<br />jj=2;<br />}<br /><br />void B::fun_b()<br />{<br />cout<<"value in A:" << ii <<"value in B:" <<jj;<br />}<br />void B::change_ii()<br />{<br />ii=5;<br />}<br /><br />// suppose both the functions are called and the value of ii is 5 and jj is 2<br />//Now I want to access the data ii from and jj from the C class.<br />//I want to assign kk a member of C class or a simple variable the value of ii (i.e. 5).<br /><br />How to go about it ?
If declare a member of type B in our C class..it will carete another instance of B but does not contains the data in the live B object.
Is passing a reference to the B class in the contructor of C class is asolution? If so, whow to do that. I am getting confused in implementin this concept.
And what other ways are there?Thanks and regards,
Alex
- AuthorPosts
Viewing 0 reply threads
- The forum ‘C Programming’ is closed to new topics and replies.