• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Lecture09
 

Lecture09

on

  • 137 views

 

Statistics

Views

Total Views
137
Views on SlideShare
137
Embed Views
0

Actions

Likes
0
Downloads
4
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Lecture09 Lecture09 Presentation Transcript

    • Lecture 09 Classes and ObjectsLearn about:i) More info on member access specifiersii) Constant Objects and Functionsiii) Array of Objectsiv) Objects as Function Argumentsv) Returning Objects from Functions 1/27
    • Access Specifiers• What is access specifier? – It is the one which specifies which member can be accessed at which place. – Types of access specifiers : Data Hiding • There are three namely, ob1 ob2 – Private Friend Ob – Public » A classs public members can be accessed by any function in a program. Used as an interface of the class - Other objects can use it . – Protected. 2/27
    • Member access specifiers (page164-176)Kitty friend of Kitty Snoopypublic public publicprivate private private 3/27
    • How to access the members ?• If the data members of the class is – Private : • Cannot access directly outside the class • How to access ? – We should use member functions to access it. – EG : data member ----> int a; » int main( ) » { object name.member function( argument ) } – Public : • can access outside the class • EG : object name . Datamembername = value; » ob1.a = 9; 4/27 Example - Next slide
    • A Simple Class#include <iostream.h>class Student // class declaration{ can only be accessed within class private: int idNum; //class data members double gpa; accessible from outside and within class public: void setData(int id, double result) { idNum = id; gpa = result; } void showData() { cout << ”Student Id is ” << idNum << endl; cout << ”GPA is " << gpa << endl; }}; 5/27
    • A simple program example#include <iostream.h> void main() {class Student Student s1, s2, s3;{ s1.setData(10016666, 3.14); public: s2.setData(10011776, 3.55); int idNum; double gpa; s3.idNum = 10011886; void setData(int, double); s3.gpa = 3.22; void showData();}; : : Okay, because idNum } and gpa are public.//Implementation - refer to notes page 1(Lecture 9). 6/27
    • A simple program example#include <iostream.h> void main() {class Student Student s1, s2, s3;{ s1.setData(10016666, 3.14); private: s2.setData(10011776, 3.55); int idNum; double gpa; s3.idNum = 10011886; public: s3.gpa = 3.22; void setData(int, double); void showData(); :}; : Error!!! idNum } and gpa are private.//Implementation - refer to notes page 1 7/27(Lecture 9).
    • Another program exampleclass student { private: char sid[20]; char name[20]; int semester; int year; float marks[4]; float average; public:// … student (char [], char [], int sem =1 , int yr = 1); void print( ) ; void compute_average( ) { average = (marks[0]+ marks[1]+ marks[2]+ marks[3])/4; }}; 8/27
    • Another program example// class function members implementation default argumentsstudent :: student (char id[20], char nama[20], int sem =1 , int yr = 1 ) { strcpy (name, nama); strcpy (sid, id); year = yr; semester = sem; }void student :: print ( ){ cout << “Student Id:”<<sid<<endl; cout << “Student Name:”<<name<<endl; cout << “Semester:”<<semester<<endl;} 9/27
    • Private student members sid name ... average ... marks Private members cannot be semester accessed from outside the class year That’s why, we use public methods to access them student (char [] id, char [] nama, int sem =1 , int yr = 1) compute_average(...) set_marks (...) void print() .. void compute_average() . .. void set_marks. (float [])Public members 10/27
    • Another program examplemain (){ student a_given_student (“9870025”, “Omar”, 2, 2);// This is instantiation.//The object a_given_student is created and the constructor is//automatically called to initialise that data members of a_given_student with// I want to change his semester to 3a_given_student. semester = 3 // ILLEGAL// why? Because semester is a private data member and cannot//be accessed outside the class student...} 11/27
    • Another program example Then, how can we access semester and change it? 2 options: Option 1 We must define a new function member (public) to access semesterclass student{ private: main () …. { student a_given_student (“9870025”, “Omar”, 2, 2); int semester; … // I want to change his semester to 3public: … a_given_student. set_semester( 3 ) ;void set_semester(int sem =1) … {semester = sem;} } …}; 12/27
    • Another program example Option 2 We must change the access specifier of semester, and make it public.class student main (){ private: { …. student a_given_student (“9870025”, “Omar”, 2, 2); public : int semester; … // I want to change his semester to 3public: … a_given_student.semester = 3; void set_semester(int sem =1) … {semester = sem;} }...}; no need! 13/27
    • Constant Object• Some objects need to be modifiable and some do not.• The keyword const is used to specify an object is not modifiable, and any attempt to change the object will produce a syntax error. For example const Model m1; or Model const m1;• const objects can only invoke their const member functions. void showInfo() const { ……. }• const declaration is not required for constructors and destructors of const objects Model () const { ……… } 14/27
    • Constant objects#include <iostream.h> void main()class Part {{ private: Part part1(8678, 222, 34.55); int modelnumber; int partnumber Part const part2; double cost; part2.setpart(2345, 444, 99.90); public: Part(int mn, int pn, double c); part1.showpart(); part2.showpart() void setpart(int mn, int pn, double c)const; } void showpart() const;};// Implementation - refer to page 5Lecture 9 15/27
    • Constant objectsclass Student{private: int age; int semester; int year;public: Student (int, int , int); // constructor void set_semester(int sem =1){semester=sem;} //to set semester with sem (1 by default) void print( ) const;}; // print is a constant function member // meaning, it cannot modify any of 16/27 // the data members
    • Constant objects Student::Student (int sem=5, int ag=20, int yr =1) { age=ag; year = yr; semester = sem; } void Student::print( ) const { cout <<"AGE:"<<age<<endl <<"Year:"<<year<<endl<<"semester:"<<semester<< endl; semester = 10; // illegal } We cannot set any data member with a value why?Cause print is const and therefore can just access data memberswithout altering their values 17/27
    • Constant objects int main() Normally invalid { why? student h1; const student h2 (4,4,4); // or student const h2(4,4,4); h1.set_semester(5); // valid h2.print(); // valid h2.set_semester(5); h2.print(); h1.print(); Cause we choose h2 as a constant object } SO, there should be no member function that can alter its data membersEXCEPTION, constructors can still be used by constant objects 18/27
    • Constant objects• Constructors and destructors are inherently non constant functionmembers• Choose your function members that just query data membersto be constants Syntax: return_type function_name (type1 par1, ..) const {… }• If you choose to work with constant objects, normally all (butconstructor and destructor) your function members should beconstants 19/27
    • Array of Objects (page87-88)class Student Student{ private: sid name char sid[20]; ... ... char name[20]; sem int sem; Student(...) public: void print() Student (char [], char [], int); void print( ) ; void set_semester(int) . void set_semester(int s ) . . { sem = s;}}; 20/27
    • int main (){student FIT[3]; // declares an array called FIT with three elements // the elements are objects of type (class) studentFIT[1].set_semester(2); // access the 2nd object of the array // and invokes its method member to set // its data member...} 21/27
    • Array Name FIT index 0 1 2 sid name sid name sid name ... ... ... ... ... ... sem sem 3 sem student(...) student(...) student(...) void print() void print() void print() void set_semester() void set_semester() void set_semester() 1st array element 2nd array element 3rd array elementI want to set the semester of the 2nd array element with 3 22/27 FIT[1].set_semester(3)
    • Objects As Function Arguments: A Diagram (page133-144) dist3.add_dist(dist1, dist2)dist3 feet inches dist1 dist2 feet inches feet inches add_dist(Distance, Distance); add_dist(Distance, Distance); inches = dist1.inches + dist2.inches; add_dist(Distance, Distance); 23/27
    • Objects As Function Arguments: A Program#include <iostream.h>class Distance{ public: Distance(); // default constructor Distance(int, float); // two-argument constructor void getdist(); void showdist(); void add_dist(Distance, Distance); private: int feet; float inches;}; 24/27
    • Objects As Function Arguments: A ProgramDistance::Distance() { feet = 0; inches = 0; }Distance::Distance(int ft, float in): feet(ft),inches(in){ }void Distance::getdist(){ cout << "nEnter feet: "; cin feet; cout << "Enter inches: "; cin inches;}void Distance::showdist(){ cout << feet << "-" << inches << ";} 25/27
    • Objects As Function Arguments: A Programvoid Distance::add_dist(Distance d1, Distance d2){ inches = d1.inches + d2.inches; feet = 0; if (inches = 12.0) { inches = inches - 12.0; feet++; } feet = feet + (d1.feet + d2.feet);} 26/27
    • Objects As Function Arguments: A Programint main(){ Distance dist1, dist3; Distance dist2(11, 6.25); dist1.getdist(); //get dist1 from user dist3.add_dist(dist1, dist2); cout << "ndist1 = "; dist1.showdist(); cout << "ndist2 = "; dist2.showdist(); cout << "ndist3 = "; dist3.showdist(); cout << endl; return 0; dist1 dist2 dist3} feet = 0 feet = 11 feet = 0 inches = 0 inches = 6.25 inches = 0 Distance() Distance() Distance() Distance(int, float) Distance(int, float) Distance(int, float) void getdist() void getdist() void getdist() void showdist() void showdist() void showdist() void add_dist() void add_dist() 27/27 add_dist() void
    • Returning Objects From Functions A Diagram dist3 = dist1.add_dist(dist2)dist1 feet inches dist2 temp feet inches feet inches add_dist(Distance); add_dist(Distance); temp.inches = inches + dist2.inches; return temp; add_dist(Distance); 28/27
    • Returning Objects From Functions Exampleclass Distance{ . . . Distance add_dist(Distance);};Distance Distance::add_dist(Distance d2){ Distance temp; temp.inches = inches + d2.inches; if(temp.inches = 12.0) { temp.inches -= 12.0; temp.feet = 1; int main() } { . . . temp.feet += feet+d2.feet; dist3=dist1.add_dist(dist2); return temp; . . .} } 29/27