C - Unit-I
C - Unit-I
Dr O.V.Shanmuga Sundaram
Associate Professor in Mathematics
1
Object-Oriented Programming
Introduction to Classes
• Class Definition
• Class Examples
• Objects
• Constructors
• Destructors
2
Class
• The class is the cornerstone of C++
– It makes possible encapsulation, data hiding and inheritance
• Type
– Concrete representation of a concept
• Eg. float with operations like -, *, + (math real numbers)
• Class
– A user defined type
– Consists of both data and methods
– Defines properties and behavior of that type
• Advantages
– Types matching program concepts
• Game Program (Explosion type)
– Concise program
– Code analysis easy
– Compiler can detect illegal uses of types
• Data Abstraction
– Separate the implementation details from its essential properties
3
Classes & Objects
Objects: Instance of a class
class Rectangle
{
Rectangle r1;
private: Rectangle r2;
int width; Rectangle r3;
……
int length;
public:
void set(int w, int l);
int area(); int a;
};
4
Define a Class Type
6
Static Data Member
Rectangle r1;
class Rectangle Rectangle r2;
{ Rectangle r3;
private:
int width;
count
int length;
static int count; r1 r2
public: width width
length length
void set(int w, int l);
int area(); width
r3 length
} 7
Class Definition
Member Functions
• Used to
– access the values of the data members (accessor)
– perform operations on the data members
(implementor)
• Are declared inside the class body
• Their definition can be placed inside the class
body, or outside the class body
• Can access both public and private members of
the class
• Can be referred to using dot or arrow member
access operator
8
Define a Member Function
class Rectangle
{
private:
int width, length;
class name
public:
void set (int w, int l);
int area() {return width*length; } member function name
};
10
Const Member Function
class Time
{
private : function declaration
int hrs, mins, secs ;
public :
void Write ( ) const ; function definition
};
11
Class Definition - Access Control
• Information hiding
– To prevent the internal representation from direct
access from outside the class
• Access Specifiers
– public
• may be accessible from anywhere within a program
– private
• may be accessed only by the member functions, and friends
of this class
– protected
• acts as public for derived classes
• behaves as private for the rest of the program
12
class Time Specification
class Time
{
public :
void Set ( int hours , int minutes , int seconds ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initHrs, int initMins, int initSecs ) ; // constructor
Time (); // default constructor
private :
int hrs ;
int mins ;
int secs ;
};
13
13
Class Interface Diagram
Time class
Set
Private data:
Increment
hrs
Write
mins
Time secs
Time
14
Class Definition
Access Control
• The default access specifier is private
• The data members are usually private or protected
• A private member function is a helper, may only be
accessed by another member function of the same
class (exception friend function)
• The public member functions are part of the class
interface
• Each access control section is optional,
repeatable, and sections may occur in any order
15
What is an object?
OBJECT
set of methods
Operations (member functions)
16
Declaration of an Object
class Rectangle
main()
{ {
private: Rectangle r1;
Rectangle r2;
int width;
int length; r1.set(5, 8);
public: cout<<r1.area()<<endl;
int main(void) {
circle c; // an object of circle class
c.store(5.0);
cout << "The area of circle c is " << c.area() << endl;
c.display(); 18
}
Declaration of an Object
r1 is statically allocated
class Rectangle
{ main()
private: {
Rectangle r1;
int width;
r1.set(5, 8);
int length;
}
public:
void set(int w, int l); r1
width = 5
int area(); length = 8
};
19
Declaration of an Object
r2 is a pointer to a Rectangle object
class Rectangle
{ main()
{
private: Rectangle r1;
int width; r1.set(5, 8); //dot notation
}
22
Object Initialization
#include <iostream.h>
class circle
{ 2. By Public Member Functions
private:
double radius;
public:
void set (double r)
{radius = r;}
double get_r ()
{return radius;}
};
int main(void) {
circle c; // an object of circle class
c.set(5.0); // initialize an object with a public member function
cout << "The radius of circle c is " << c.get_r() << endl;
// access a private data member with an accessor 23
}
Declaration of an Object
r2 is a pointer to a Rectangle object
class Rectangle
{ main()
{
private: Rectangle r1;
int width; r1.set(5, 8); //dot notation
24
Object Initialization
3. By Constructor
class Rectangle
{
private: • Default constructor
int width; • Copy constructor
int length;
• Constructor with parameters
public:
Rectangle();
Rectangle(const Rectangle &r); They are publicly accessible
Rectangle(int w, int l); Have the same name as the class
void set(int w, int l); There is no return type
int area(); Are used to initialize class data
} members
They have different signatures
25
Object Initialization
When a class is declared with no constructors,
the compiler automatically assumes default
class Rectangle constructor and copy constructor for it.
{
• Default constructor
private:
int width;
Rectangle :: Rectangle() { };
int length;
public:
• Copy constructor
void set(int w, int l);
int area(); Rectangle :: Rectangle (const
Rectangle & r)
}; {
width = r.width; length = r.length;
26
};
Object Initialization
• Initialize with default constructor
class Rectangle
Rectangle r1;
{
Rectangle *r3 = new Rectangle();
private:
int width;
• Initialize with copy constructor
int length;
public: Rectangle r4;
void set(int w, int l); r4.set(60,80);
int area(); Rectangle r5 = r4;
} Rectangle r6(r4);
Rectangle *r7 = new Rectangle(r4);
27
Object Initialization
Private: Private:
data members data members
Private: Private:
methods methods
36
Working with Multiple Files
• To improve the readability, maintainability and
reusability, codes are organized into modules.
• When working with complicated codes,
– A set of .cpp and .h files for each class groups
• .h file contains the prototype of the class
• .cpp contains the definition/implementation of the class
37
Example : time.h
// SPECIFICATION FILE ( time .h )
// Specifies the data members and
// member functions prototypes.
#ifndef _TIME_H
#define _TIME_H
class Time
{
public:
. . .
private:
. . .
};
#endif
38
Example : time.cpp
// IMPLEMENTATION FILE ( time.cpp )
// Implements the member functions of class Time
#include <iostream.h>
#include “ time.h” // also must appear in client code
……
bool Time :: Equal ( Time otherTime ) const
// Function value == true, if this time equals otherTime
// == false , otherwise
{
return ( (hrs == otherTime.hrs) && (mins == otherTime.mins)
&& (secs == otherTime.secs) ) ;
}
. . .
39
Example : main.cpp
// Client Code ( main.cpp )
#include “ time.h”
int main()
{
……
}
main.cpp time.cpp
#include “time.h”
Compiler Compiler
main.o time.o
Linker
mainExec 41