You are on page 1of 14

5.6.

Introduction to Classes
Abstraction

An abstraction is a general model of something.
 It is a definition that includes only the general
characteristics of an object without going into all
the details that characterize specific instances of
the object.
 For example, the term “car” is an abstraction. It
defines a general type of vehicle. The term
captures the essence of what all cars are without
specifying the detailed characteristics of any
particular type of car.
Abstraction Continued

While the term car is abstract, a particular, real life car
(for example, a blue Toyota Corolla DX 1983 model, …)
is concrete.

Real life cars come in many shapes, sizes, colors, and
capabilities.

Most people have an idea of a car and they know how
to drive one. But a few know about the internal workings
of the car.

You don't need to know the details of how a car works
just to drive it.

Can you find out other real-world abstractions?
Abstract Data Type

An abstract data type (ADT) is a data type that
specifies the values the data type can hold and the
operations that can be done on them without the need
for anyone using the ADT to know how the data type
itself is implemented.

Commonly, ADT refers to programmer defined types.

The programmer defines a set of values the data type
can hold, defines a set of operations that can be
performed on the data, and creates a set of functions
to carry out these operations.

In C++, ADTs are mainly implemented as classes
Classes and Objects
 A class is similar to a structure.
 In addition to what we have seen for a
structure, a class can also contain functions.
(Note: in C++, a structure can also contain
functions but it is rarely used that way)
 Objects are instances of a class. They are
created with a definition statement after a class
has been declared.
Declaring Classes
class Student {
private: //an access specifier
char name[30];
public: //another access specifier
int age;
void setName(char [] nm) {
strcpy(name, nm);
} //the first function ends here
int getAgePlus(int plus) {
return age + plus;
} //the last function ends here
}; //the class definition ends here. Notice the semicolon
Access Specifiers
 The terms private and public in the previous
class declaration are known as access
specifiers.
 The access specifiers designate who can
access the various members of the class.
 Public: can be accessed/called by functions
outside the class
 Private: can only be accessed/called by functions
inside the class.
 Protected: Not to be discussed at this point.
Defining and Using Objects
Student me; //the object me is created
char myName[] = “Biruk”;
me.setName(myName); //setName is public
me.age = 14; //age is public
cout<<me.getAgePlus(4);

strcat(me.name, “ Wendimagegn”);
//error because name is private.
More about Objects and Classes
 A class is a template for an object.
 A class is like a blueprint (a plan) from which
objects are created.
 No memory is occupied when a class is
defined.
 The objects reside in memory.
Member Functions
 Class member functions can be defined either
inside or outside of a class declaration.
 When a member function is define inside a
class declaration, it is known as inline
function.
 We will come to this again in chapter 6.
Constructor
 A constructor is a member function that gets
executed automatically when an object is
created.
 It is used to initialize member variables.
 Constructors have the following characteristics
 They don't have a return type
 They have the same name as the class
 They are found under the public access specifier
Example
class Student { char myName[] = “Biruk”;
char name[30]; Student me(myName, 14);
int age;
public:
//can I do this?
Student(char [] nm, int a)
{ cout<<me.name;
strcpy(name, nm);
age = a;
}
//other member functions
};
More about Constructors
 A constructor that accepts no arguments is
called a Default Constructor.
 A constructor is just like any other function. i.e.
It can do more than just initialization.
 The initialization can be done alternatively by
using a member initializer list.
Example
class Demo
{
private:
int a;
int b;
public:
Demo () : a(2), b(4) {}
};

You might also like