Docsity

Friends.cpp-Object Oriented Programming-Lecture Handout, Exercises of Object Oriented Programming

Toggle sidebar
bg1
1c:\documents and settings\usman.younis\my documents... studio 2010\Projects\friends\friends\friends.cpp
# include < iostream >
using namespace std ;
class student
{
private :
int stud _ reg _ num ;
char stud _ name [ 50 ] ;
// private data , can ' t be accessed
// in main () , any other function , or a class
public :
student ()
{
stud _ reg _ num = 0 ;
strcpy ( stud _ name , "") ;
}
// to access this data in another class
// we made it frined of student
friend class my _ Class ;
};
class my _ Class
{
private :
int counter ;
student Student [ 50 ] ;
void display _ student ( int a ) ;
public :
my _ Class () : counter ( 0 ) {}
void set _ student _ val ( int r _ num , char * name ) ;
friend void display _ students _ from _ main ( my _ Class & b ) ;
};
void my _ Class :: display _ student ( int a )
{
// private member function
// Can ' t be accessed from main ()
cout << " Student registration number is : " << Student [ a ]. stud _ reg _ num << endl ;
cout << " Student name is : " << Student [ a ]. stud _ name << endl ;
}
void my _ Class :: set _ student _ val ( int r _ num , char * name )
{
// public member function
// Can be accessed from main ()
Student [ counter ]. stud _ reg _ num = r _ num ;
strcpy ( Student [ counter ]. stud _ name , name ) ;
counter ++ ;
}
void display _ students _ from _ main ( my _ Class & b )
{
// function is not a member of class
// therefore , we havent used scope resolution operator
for ( int i = 0 ; i < b . counter ; i ++)
b . display _ student ( i ) ;
}
void set _ students _ from _ main ( my _ Class & b )
docsity.com
pf2

Partial preview of the text

Download Friends.cpp-Object Oriented Programming-Lecture Handout and more Exercises Object Oriented Programming in PDF only on Docsity!

c:\documents and settings\usman.younis\my documents... studio 2010\Projects\friends\friends\friends.cpp 1 #include using namespace std; class student { private: int stud_reg_num; char stud_name[ 50 ]; //private data, can't be accessed //in main(), any other function, or a class public: student() { stud_reg_num = 0 ; strcpy(stud_name, ""); } //to access this data in another class //we made it frined of student friend class my_Class; }; class my_Class { private: int counter; student Student[ 50 ]; void display_student(int a); public: my_Class() : counter( 0 ) {} void set_student_val(int r_num, char* name); friend void display_students_from_main(my_Class& b); }; void my_Class::display_student(int a) { //private member function //Can't be accessed from main() cout<<"Student registration number is : "<<Student[a].stud_reg_num<<endl; cout<<"Student name is : "<<Student[a].stud_name<<endl; } void my_Class::set_student_val(int r_num, char* name) { //public member function //Can be accessed from main() Student[counter].stud_reg_num = r_num; strcpy(Student[counter].stud_name, name); counter++; } void display_students_from_main(my_Class& b) { //function is not a member of class //therefore, we havent used scope resolution operator for(int i = 0 ; i < b.counter; i++) b.display_student(i); } void set_students_from_main(my_Class& b)

docsity.com

c:\documents and settings\usman.younis\my documents... studio 2010\Projects\friends\friends\friends.cpp 2 { //An external function //can easily access all public members of an object char stud_name[ 50 ]; int stud_reg_num; char choice = 'y'; do { cout<<"Enter the students name : "; cin.sync(); cin.getline(stud_name, 50 ); cout<<"Enter the registration number : "; cin>>stud_reg_num; b.set_student_val(stud_reg_num, stud_name); cout<<"Add another (y/n)? "; cin>>choice; } while(choice != 'n'); } int main() { my_Class BEE; set_students_from_main(BEE); display_students_from_main(BEE); return 0 ; }

docsity.com