Vous êtes sur la page 1sur 15

Week 8-9

Outline
• Defining classes
• Members of class
•Data Members
•Member Functions
• General form of a class
• Declaring objects

1
Classes
• A class is a collection of data and
functions
• The data items and functions are
defined within the class
• Classes are defined to create user-
defined data types
• When a class is defined, it does not
occupy any space in the computer
memory
• The data items are called data
members
• 1
How to define a class
// smallobj.cpp
• 2 // demonstrates a small, simple object
• 3 #include <iostream.h>
• 4
• 5 Define a class with class
• 6
• 7
name
• 8 class smallobj
Private keyword: Private
// specify a class
• 9 {
Data item
data or of a class can only
functions
Public keyword: Public
• 10 private: be accessed
Member
data from
function
or functions within
are
• 11 int somedata; // are
class data
• 12 public:
the classfrom
functions
accessible that are included
outside
• 13 void setdata(int d) within
the class a class // member function to set data
• 14 { somedata = d; }
• 15 void showdata () // member function to display a data
• 16 { cout <<“\Data is “ <<somedata; }
• 17 };
• 19 int main() Objects: An instance of a
• 20 {
• 21 smallobj s1,s2;
class. to
Dot operator: i.e.call
s1 //define
and s2 two objects of class smallobj
the
• 22 s1.setdata(1066); member function of a//call member function to set data
• 23 s2.setdata(1776); class, is called class
• 24 s1.showdata(); //call member function to display data
• 25 s2.showdata(); member access operator
• 26 return 0; // indicates successful termination
• 27
• 28 } // end main
Data is 1066
Data is 1776
Classes and Objects
• An object is said to be an instance of a
class, in the same way my 1954 Chevrolet
is an instance of a vehicle.
• An object represents data members of a
class in the memory
• When a object is created, the space is
reserved in memory
• In SMALLOBJ, the class – whose name is
smallobj – is specified in the first part of
the program.
• Later, in main(), we define two objects s1
and s2– that are instances of that class.
General form of a class
Name of class
Keyword

class foo Braces Keyword private and


{ colon
Private functions and data
private: Keyword public and colon
– int data; Public functions and data
public:
– void memfunct (int d)
Semicolon
{ data = d; }
};
Controlling Access to
Members
• Access modes
– Private
• Data or functions can only be accessed
from within the class
• Default access mode
• Accessible to member functions and friends
– Public
• Data or functions are accessible from
outside the class
• Accessible to any function in program with
handle to class object
– Protected
7 • See later
Declaring objects
• The general syntax of defining an
object
classname object1, object2……objectn;
• An example of defining an object
smallobj s1,s2;
• The first statement in main() defines
two objects, s1 and s2 of class smallobj.
• Defining an object is same as defining a
variable of any data type, space is set
aside for it in memory
Two objects of class
smallobj smallobj class specifier

somedata

Specifications for
smallobj objects

Object of Object of
class class
s1 specifier s2 specifier
somedata somedata
1066 1776
• 1
Another example of class
// objpart.cpp
• 2 // widget part as an object
• 3 #include <iostream.h>
• 4
• 5
• 6
• 7
• 8 class part / specify an object
• 9 {
• 10 private:
• 11 int modelnumber; // ID number of widget
• 12 int partnumber; // ID number of widget part
• 13 float cost; // cost of part
• 14 public:
• 15 void setpart(int mn, int pn, float c) // set data
• 16 { modelnumber = mn;
• 17 partnumber= pn;
• 18 cost = c;
• 19 }
• 20 void showpart () // member function to display a data
• 21 { cout <<“\Model “ <<modelnumber;
• 22 cout <<“, part “ <<partnumber;
• 23 cout<< “,costs $” << cost;
• 24 }
• 25 };
• 26 int main()
• 27 {
• 28 part part1; //define object of class part
• 29 part1.setpart(6244, 373, 217.55); //call member function
• 30 part1.showpart(); //call member function to display data
• 31 return 0; // indicates successful termination
• 32
• 33 } // end main
Model 6244, part 373, costs $217.55
Passing objects as
arguments
• Objects can also be passed as arguments to
member functions
• When an object is passed as an argument to a
member function:
– Only the name of the object is written in the
argument
– The number of parameters and their types
must be defined in the member function to
which the object is passed
– The objects that are passed are treated local
for the member function and are destroyed
when the control returns to the calling function
How to pass object as an



1
2
3
argument
// objpart.cpp
// passing object as am argument to the member function
#include <iostream.h>
• 4 #include <stdio.h>
• 5
• 6
• 7
• 8 class obj // specify an object
• 9 {
• 10 private:
• 11 char name[15]; // declare an array
• 12
• 13
• 14 public:
• 15 void setdata(void)
• 16 { cout<<“Enter your name: “;
• 17 gets(name);
• 18
• 19 }
• 20 void show (obj s) // member function to display a data
• 21 { cout <<“Your Name is: “<<s.name<<endl; // pass object s1 to setdata
• 22
• 23 }
• 24 };
• 25 int main()
• 26 {
• 27 obj s1,s2;; //define object of class part
• 28 s1.setdata(); //call member function
• 29 s2.show(s1); //call member function to display data
• 30 return 0; // indicates successful termination
• 31
• 32 } // end main
Enter your name: shehzad rizwan
Your name is: shehzad rizwan
Passing objects as
arguments (cont)
• In the previous example, the class “obj” has
one data member “name” and two member
functions “setdata()” and “show()”
• The “show()” function has an argument “s”
of class “obj” type
• When a function “show()” is called from the
main(), object “s1” is passed as an
argument to the function “show()”. i.e. a
copy of “s1” is created as a local object “s”
• Member function “show()” gets the name in
the object “s1” and store it into the data
member “name”

Vous aimerez peut-être aussi