Sei sulla pagina 1di 20

C++ Class

User Defined Data Type


C provides struct, enumeration, union
C++ provides struct, enumeration, union,

class
Objective is to define new object and define
new behavior
For example, birds flying action, withdrawing
money from ATM
It can be implemented in programming
language by putting built-in data type
together and defining new operations

Class Basics
User-defined data type
Consists of a set of members (data members &

member functions)
Data members store the state of the object
Member functions can define the initialization, copy,
move, behavior and cleanup
Members are accessed using . (dot) for objects and
-> (arrow) for pointers
Class is a namespace containing its members
Public members provide classs interface and private
members provide implementation details

Member Functions
In programming language implementing a

concept requires two things:


1. Define the representation
2. Define set of functions operating

(manipulating) on this representation


(variables)
For example: the concept of a date using a

struct can be implemented in following way


struct Date {
int d, m, y;
}

// representation
// data members

Member Functions (Contd)


Functions to operate on Date object
void
void
void
void

init_date(Date &d , int, int, int); //initialize d


add_year(Date &d, int n);
//add n years
add_month(Date &d, int n);
//add n months
add_day(Date &d, int n);
//add n days

No explicit connection between data type

Date and
functions

Member Functions (Contd)


Declare functions as members:

Curly braces introduce


new scope. Init and
add_year can operate
only on datatype Date

struct Date {
int dd, mm, yy;
void init(int d, int m, int y); // initialize
void add_year(int n); // add n years
};

Functions declared within a struct definition

are called member functions


Member functions can be invoked only for a
specific variable of the appropriate type

Member Functions (Contd)


Function header for Member Function uses

scope resolution operator :: to identify the


class to which the function belongs
const char * Student::Name(char *n){}

Identifier Name() has class scope


Member functions can access the private

components of the class


Member functions can call other member
functions without using scope resolution
operator ::

Member Functions (Contd)


Member function definition
invocation

void Date::init (int d, int m,


int y)
Scope
{
resolution
Operator
dd = d;
resolves the
mm = m;
identity of
class
yy = y;
}
void Date::add_day (int d)
{
dd += d;
}

Member function
Date my_birthday;
void f() {
Date today;
today.init(16, 10,
1996);
my_birthday.init(30, 12,
1950);
Date tomorrow =
today;
tomorrow.add_day(1);

Member Functions (Contd)


Which objects dd member is modified by

void Date::add_day() in previous example?


Either
today or tomorrow ??????
Membership operator . resolves which
object to operate on
When Date::init() is invoked for object today
using today.init() statement, mm=m assigns
to today.mm
Member function uses the data members of
the particular object used to invoke the
member function

Member Functions (Contd)

Member Functions (Contd)


In member functions, member can be used

without explicit reference to an object


Name refers to that member of the object for
which the function was invoked
Void f() {
Date today;
today.init(16, 10, 1996);

d = 16,

dd = 234556
mm =
87234901
yy = 19342

Address:
oxfacdbe2102
void Date::init(this = oxfacdbe2102,
m = 10, y = 1996)

{
dd = d;

// this is equal to

Member Functions (Contd)


Member functions can call other member

functions without using scope resolution


operator
::
For example,
struct Book {
char name[20];
char author[20];
char isbn[15];

Void Book::init(const char *n,


const char *auth,
const char *i) {

void init(const char


*n,
*auth,
*i);

const char name


const char name
}
void update_name();
void update_isbn();

.
.
update_name(); //unqualified
Book::update_isbn(); //qualified

Access Control
Declaration of Date in example provides a set

of functions for manipulating a Date


It does not specify that those functions are the
only ones to directly access objects of class
Date and modify data members
For example,
void timewrap(Date& d) {
operator

// note that scope resolution


is not used, timewrap is in

global scope
d.dd = d.dd 200;
since timewrap

// explicit reference needed


does not know which

object to work on

Access Control (contd)


Can we avoid access violation?
Can we restrict access to data members to set

of functions?
Yes, keep data members in private section
struct Date {
private:
int d, m, y;
public:
void init(int m, intd, int y);
void add_year(int n);
}

By default structs members are public

Access Control (Contd)


Data members define representation of an

object
Member functions define interface to object
Private part can be used only by member
functions
Public part constitutes the public interface to
For
example,
objects
Non-member
functions can
void timewrap(Date
&d) {not access private
d.y = d.y 200;
//error: Date::y is private
member
member

Access Control (Contd)


Making members private forces to provide a way

of initializing members
Init() function can do this job
Date dx;
dx.mm = 3; // error
dx.init(25, 3, 2011); // OK

Is it convenient?
Every object declaration requires following init()

function to make sure initialization happen


properly
In large projects this becomes cumbersome and
error prone

Benefits of Private members


Object corruption or illegal value by non

member functions can be avoided


Only member functions can cause illegal
value. Easy to debug such problem
Change in classs representation requires
changes in member functions only
Classs user code directly depends only on the
public interface and need not be rewritten
Developer can focus more on the interface
design instead of debugging

Object Initialization
Can we do better?
Yes, we can. Take the help of Constructor
Constructor is a method with explicit purpose

of initializing object
It is recognized by having the same name as
the class itself
For example,
struct Date {
private:
int m, d, y;
public:
Date();
Date(int mm, int
dd, int yy);

int main() {
Date now;
Date tomorrow(11, 3,
2016);
}

Where is the Class?


Substitute class in place of struct

END

Potrebbero piacerti anche