[go: up one dir, main page]

0% found this document useful (0 votes)
45 views41 pages

C++ Object-Oriented Programming Basics

Uploaded by

yadim96622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views41 pages

C++ Object-Oriented Programming Basics

Uploaded by

yadim96622
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Lecture 6

Introduction to Classes

Class in C++ 1
Procedural versus Object-Oriented
Programming
• Procedural programming focuses on the
process/actions that occur in a program. The
program starts at the beginning, does
something, and ends.

• Object-Oriented programming is based on the


data and the functions that operate on it.
Objects are instances of abstract data types
that represent the data and its functions
Class in C++ 2
Key Point
• An object or class contains the data and the
functions that operate on that data. Objects
are similar to structs but contain functions, as
well.

Class in C++ 3
Limitations of Procedural
Programming
• If the data structures change, many
functions must also be changed

• Programs that are based on complex


function hierarchies are:
– difficult to understand and maintain
– difficult to modify and extend
– easy to break

Class in C++ 4
Object Oriented concepts

1. Objects
2. Classes
3. Data abstraction and encapsulation
4. Inheritance
5. Polymorphism
6. Dynamic binding
7. Message passing
Object-Oriented Programming
Terminology
• class: like a struct (allows bundling of
related variables), but variables and functions
in the class can have different properties than
in a struct
• object: an instance of a class, in the same
way that a variable can be an instance of a
struct

Class in C++ 6
Classes and Objects
• A Class is like a blueprint and objects are like
houses built from the blueprint

Class in C++ 7
Object-Oriented Programming
Terminology
• attributes: members of a class

• methods or behaviors: member functions of a


class

Class in C++ 8
More Object Terms
• data hiding: restricting access to certain
members of an object

• public interface: members of an object that


are available outside of the object. This allows
the object to provide access to some data and
functions without sharing its internal details
and design, and provides some protection
from data corruption

Class in C++ 9
Creating a Class
• Objects are created from a class
• Format:
class ClassName
{
declaration;
declaration;
};

Class in C++ 10
Objects

• Objects are the basic run-time entities in an object-


oriented system.
• Objects will have some attributes or characteristics and
that makes it different from others.
• They may represent a person, a place, a bank account, a
table of data or any item that the program must handle.
• The fundamental idea behind object oriented approach
is to combine both data and function into a single unit
and these units are called objects.
Class in C++ 12
UML Class and Instance diagram

Class in C++ 13
Class

• A group of objects that share common properties for


data part and some program part are collectively
called as class.
• In C ++ a class is a new data type that contains
member variables and member functions that operate
on the variables.
How to declare a class

class classname
class classname
{ {
variable declaration; variable declaration;
function declaration; function declaration;
}; or } object;
main()
{ main()
classname object; {
} -------
}
The variables declared inside the class are known as data members and functions are known as member functions
Process of creating an object is called instantiation. Object is an instance of a class.
Data Encapsulation
• The wrapping up of data and function into a
single unit (called class) is known as
encapsulation.
• The data is not accessible to the outside world
and only those functions which are wrapped
in the class can access it.
• The feature encapsulation is supported using
class.
Abstraction
• Abstraction refers to the act of representing
essential features without including the
back ground details or explanations.
• Since class use the concept of data
abstraction, it is called as Abstract Data
Type(ADT).
Methods and Messages

• A method is a function that is part of the class definition.


• The methods of a class specify how its objects will respond to any
particular message.

• A message is a request, sent to an object, that activates a method


(i.e., a memberfunction).
The Class Body
class classname
{
private:
• Within the body, the keywords ...
private members
private: and public:specify the ... or methods
access level of the members of ...
the class. public:
• the default isprivate ...
public members
• Usually, the data members of a ... or methods
class are declared in the private: ...
section of the class and the
member functions are in the };
public: section.
Access Specifiers
Used to control access to members of the class

• Data members or member functions may be public, private or


protected
• Public
• Members can be accessed outside the class directly
• Acts as an interface
• Private
• Accessible only to member functions of the class
• Private members and methods are only for internal use within
the class and cannot be accessed outside the class
• Protected
• Data members and member functions can be used in the same
class and its derived class (for one level)
• Cannot be used in main function
Example – defining inside
#include<iostream>
class
void putData(){
using namespace std;
cout<<rollNo<<" "<<name;
class student{
}
int rollNo;
char name[20]; };
public:
void readData(){
cin>>rollNo;
cin>>name;
}
Implementing Class
Methods
• Inside the class -- straightforward
• Outside the class
• Using binary scope resolution operator (::)
• Connects member name to class name
• Uniquely identifies functions of a particular class
• Different classes can have member functions with same
name
• Format:
ReturnType ClassName :: MemberFunctionName(){
...
}

• A member function can call another member function without


using the dotoperator
Example – defining outside
class Scope Resolution
Operator
#include<iostream> using namespace Return Type
std; Member Function
class student{ Class Name
Name
int rollNo;
char name[20];
public:
void student :: putData(){
void readData(); cout<<rollNo<<" "<<name;
}
void putData();
}; void student :: readData(){
cin>>rollNo; cin>>name;
}
Class Example

Class in C++ 27
Access Specifiers
Private Members

Public Members

Class in C++ 28
Access Specifiers (continued)
• Can be listed in any order in a class

• Can appear multiple times in a class

• If not specified, the default is private

Class in C++ 29
Using const With Member Functions
• const appearing after the parentheses in a
member function declaration specifies that
the function will not change any data in the
calling object.

Class in C++ 30
Defining a Member Function
• When defining a member function:
– Put prototype in class declaration
– Define function using class name and scope
resolution operator (::)

int Rectangle::setWidth(double w)
{
width = w;
}

Class in C++ 31
Accessors and Mutators
• Mutator/setters: a member function that
stores a value in a private member variable, or
changes its value in some way

• Accessor/getters: function that retrieves a


value from a private member variable.
Accessors do not change an object's data, so
they should be marked const.

Class in C++ 32
Defining an Instance of a Class
• An object is an instance of a class
• Defined like structure variables:
Rectangle r;
• Access members using dot operator:
[Link](5.2);
cout << [Link]();
• Compiler error if you attempt to access a
private member using dot operator

Class in C++ 33
Derived Attributes
• Some data must be stored as an attribute.
• Other data should be computed. If we stored
“area” as a field, its value would have to
change whenever we changed length or
width.
• In a class about a “person,” store birth date
and compute age

Class in C++ 34
Private Members
• Making data members private provides
data protection

• Data can be accessed only through public


functions

• Public functions define the class’s public


interface

Class in C++ 35
Private Members
Code outside the class must use the class's
public member functions to interact with the
object.

Class in C++ 36
Inline Member Functions
• Member functions can be defined
– inline: in class declaration
– after the class declaration

• Inline appropriate for short function bodies:


int getWidth() const
{ return width; }

Class in C++ 37
Tradeoffs – Inline vs. Regular Member
Functions
• Regular functions – when called, compiler
stores return address of call, allocates
memory for local variables, etc.

• Code for an inline function is copied into


program in place of call – larger executable
program, but no function call overhead, hence
faster execution

Class in C++ 38
#include <iostream>
using namespace std;
class Rectangle
{ private:
double length; //length of rectangle
double width; //breadth of rectangle
public:
void setLength( double );
void setWidth( double );
double getArea();
}; int main()
{
void Rectangle::setLength( double l ) Rectangle rt;
{
length = l;
[Link](7);
} [Link](4);
void Rectangle::setWidth( double b ) double area = [Link]();
{ cout << "Area : " << area << endl;
width = b;
}
return 0;
double Rectangle::getArea() }
{
return length * width;
}

Class in C++ 39
Use of const for accessor functions
#include <iostream>
/* defining member functions */
using namespace std; void Rectangle::setLength( double l
)//mutator/setter
class Rectangle {
{
length = l;
}
void Rectangle::setWidth( double b )// mutator
private:
{
/* declaring data members */
width = b;
double length; //length of rectangle
}
double width; //breadth of rectangle double Rectangle::getArea()const //accessor/getter
public: {
/* declaring member functions */ return length * width;
void setLength( double ); }
void setWidth( double ); double Rectangle::getLength() const //accessor
double getArea()const; {
double getLength()const; return length ;
double getWidth()const; }
}; double Rectangle::getWidth() const //accessor
{
return width;
}
Class in C++
class Rectangle
{ See the use of constructor and destructor
private:
/* declaring data members */
double length; //length of rectangle
double width; //breadth of rectangle
public:
/* declaring member functions */
Rectangle() //default constructor
{
cout<<"Hello I am constructor";
}
~Rectangle() //destructor
{
cout<<"Hello I am destructor";
}

void setLength( double );


void setWidth( double );
double getArea()const;
double getLength()const;
double getWidth()const;
• };

Class in C++ 41

You might also like