Lecture - 2
Classes in C++
CS223- Object Oriented Programming
Example
Person
Data: name, age, gender, bank accno.,…
Methods: getname(),getage(),…
LaptopVendor
Data: stock,supplier,actual_cost,…
Methods: getquotation(),buy(),…
Circle
Data: radius
Methods: getradius(),getarea(),…
17
Ghulam Ishaq Khan Institute – Spring 2012
Classes (in C++)
A class definition begins with the keyword class.
The body of the class is contained within a set of braces, {
} ; (notice the semi-colon).
class class_name
{ Any valid
…. identifier
….
…. Class body (data member
}; + methods)
18
Ghulam Ishaq Khan Institute – Spring 2012
Classes (in C++)
Within the body, the keywords private: and public: specify
the access level of the members of the class.
the default is private.
Usually, the data members of a class are declared in the
private: section of the class and the member functions are in
public: section.
19
Ghulam Ishaq Khan Institute – Spring 2012
Classes (in C++)
class class_name
{
private: private members or
… methods
…
…
public:
… Public members or methods
…
…
};
20
Ghulam Ishaq Khan Institute – Spring 2012
Example of a Class (in C++)
class someobject //declares a class
{
private:
int somedata; //class data
public:
void setdata(int d) //membership function to set data
{ somedata=d; }
int getdata() //membership function to get data
{ return somedata; }
}
21
Ghulam Ishaq Khan Institute – Spring 2012
Classes in C++
Member access specifiers
public:
can be accessed outside the class directly.
The public stuff is the interface.
private:
Accessible only to member functions of class
Private members and methods are for internal use only.
22
Ghulam Ishaq Khan Institute – Spring 2012
Classes in C++
This class example shows how we can encapsulate (gather) a
circle information into one package (unit or class)
No need for others classes to
class Circle access and retrieve its value
{ directly. The class methods are
responsible for that only.
private:
double radius;
public:
void setRadius(double r); They are accessible from outside
double getDiameter(); the class, and they can access the
double getArea(); member (radius)
double getCircumference();
};
23
Ghulam Ishaq Khan Institute – Spring 2012
Classes and Objects
A class is a prototype specification from which
one can generate a number of similar objects
A class can be considered as an object factory.
An object is said to be a member or instance of a class
A class can be considered as a more complex
data structure than an ordinary built-in data type
Standard C already knows the struct command for user
defined data types:
24
Ghulam Ishaq Khan Institute – Spring 2012
Implementing Member Functions
2 ways of writing the code of class methods.
1. Member functions defined outside class
Using Binary scope resolution operator (::)
Uniquely identify functions of particular class
Different classes can have member functions with same name
Format for defining member functions
2. Member functions defined inside class
Do not need scope resolution operator, class name;
25
Ghulam Ishaq Khan Institute – Spring 2012
Case 1: Outside the Class
class Circle
{
private:
double radius;
public:
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
double Circle::getArea()
Defined outside class
{
return radius * radius * (22.0/7);
}
double Circle:: getCircumference()
{
return 2 * radius * (22.0/7);
}
26
Ghulam Ishaq Khan Institute – Spring 2012
Case 2: Inside the Class
class Circle Defined
{ inside
private: class
double radius;
public:
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
};
27
Ghulam Ishaq Khan Institute – Spring 2012
Accessing Class Members
Operators to access class members
Identical to those for structs
Dot member selection operator (.)
Object
Reference to object
Arrow member selection operator (->)
Pointers
28
Ghulam Ishaq Khan Institute – Spring 2012
Example
class Circle
{
private:
double radius;
public:
void setRadius(double r){radius = r;}
double getDiameter(){ return radius *2;}
double getArea();
double getCircumference();
}; void main()
{
Circle c;
double Circle::getArea() Circle *cp1 = &c;
{ [Link](7);
return radius * radius * (22.0/7);
} char* str1="Area of circle is ";
double Circle:: getCircumference() char* str2 =" and its diameter is ";
{ cout<<str1<<cp1->getArea()<<
return 2 * radius * (22.0/7); str2<<[Link]()<<endl;
char ch=getche();
} 29
}
Ghulam Ishaq Khan Institute – Spring 2012
Summary
1. Different entities are objects
2. Interaction happens between objects (like in real life)
3. Computation: sending & receiving messages (requests +
arguments)
4. Each Object has its own memory (other objects)
5. Every object is instance of some class.
6. Class is a repository for behaviors.
30
Ghulam Ishaq Khan Institute – Spring 2012
Course Text Books
C++
C++ - How to Program
Deitel & Deitel
The Waite’s Group’s Object-Oriented Programming in C++,
Robert Lafore
Java
Java- How to program (recommended)
Deitel & Deitel, Prentice Hall.
Beginning Java 2, JDK 5 Edition
Ivor Horton
31
Ghulam Ishaq Khan Institute – Spring 2012
Reference Material
Object Oriented Design and UML
Design Patterns,Addison-Wesley
Erich Gamma
The Unified Modeling Language User Guide,
Grady Booch,Addison-Wesley
UML Distilled,Addison-Wesley
Martin Fowler
32
Ghulam Ishaq Khan Institute – Spring 2012