0% found this document useful (0 votes)
5 views32 pages

Understanding C++ Objects and Classes

Uploaded by

akmcy1601
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)
5 views32 pages

Understanding C++ Objects and Classes

Uploaded by

akmcy1601
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

Yangon Technological University

Department of Computer Engineering and Information Technology

Topic 2: Objects and Classes

Dept. of Computer Engineering and Information Technology


Yangon Technological University

1/32
Course Contents
Topic 1 Overview of C++, Operators and Control Statements

Topic 2 Objects and Classes

Topic 3 Polymorphism and Operator Overloading

Topic 4 Inheritance

Topic 5 Templates and Exception Handling

Topic 6 Streams and Files


2/32
Course Contents
Topic 1 Overview of C++, Operators and Control Statements

Topic 2 Objects and Classes

Topic 3 Polymorphism and Operator Overloading

Topic 4 Inheritance

Topic 5 Templates and Exception Handling

Topic 6 Streams and Files


3/32
Topic 2
Objects and Classes

4/32
Lecture Objectives

To introduce
What is an Object?
What is the Class?
Defining the Classes & Objects
Access Control Specifiers
Constructors and Destructors
To implement a C++ program with Classes

5/32
What is an Object?

Object are everything in the real world

4 wheels
attribute, states
2 doors (data)
backward
move
rotate Behaviour
forward (function)

Car

6/32
What is an Object?

Another way
Variables or instances of a class is declared in a program

OBJECT

set of methods
(public member functions)
Operations
internal state
Data
(values of private data members)

7/32
What is the Class?

A description of one or more objects with a uniform set of attributes and


services including a description of how to create new objects in the Class
So, a class is to be a kind of template
state
Obj
function
Obj state
CLASS
function
Obj state
function
4 doors
Data
Car
4 tires
Method Move/stop
8/32
Defining the Class &Object

Class
 Defines properties and behavior for objects
 Consists of both data and functions/methods

Object
 An instance of class
 Object-oriented programming (OOP) involves programming using
objects
o An object represents an entity in the real world
9/32 o An object has a unique identity, state, and behavior
Syntax of Class

Syntax of class class age age

class class_name { { Data: age


access_specifier_1: private: Method: get_age()
data member; int age;
access_specifier_2: public:
member function; void get_age()
}; {
cin >> age;
}
};

10/32
Accessing an Object’s Data &Methods

 An object’s member refers to its data fields and methods


 After an object is created, its data can be accessed and its methods
invoked
using the dot operator (.)

[Link]

[Link](arguments)

11/32
Example 1
int main ()
// classes example
{
#include <iostream>
using namespace std; Rectangle rect;
class Rectangle rect.set_values (3,4);
{ cout << "area: "<< [Link]();
private: return 0;
int x, y;
public: }
void set_values (int a, int b)
{x = a; y = b; }
int area()
{
return (x*y);
} Output
}; area: 12

12/32
Example 2 Scope resolution
operator

void Rectangle::set_values (int a, int b)


// written the function outside the class { x = a; Written the
#include <iostream> y = b; function outside
using namespace std; the class
class Rectangle }
{ int main ()
private: {
int x, y; Rectangle rect;
public: rect.set_values (3,4);
void set_values (int, int);
int area () cout << "area: "<< [Link]();
{ return 0;
return (x*y); }
}
};
Output
area: 12
13/32
Example 3
// classes example int area ()
#include <iostream> {
using namespace std; return (x*y);
class Rectangle }
{ };
private: int main ()
int x, y; {
public: Rectangle rect;
void get_values () rect.get_values ();
{
cout << “Enter the first value\n”; cout << "area: "<< [Link]();
cin >> x; return 0;
cout << “Enter the second value\n”; }
cin >> y;
} Output
area: ???
14/32
Class includes:

 Data Members (The data item within a class)

 Member Functions (Functions that are included within a class)

 Access Control Specifiers

 Constructors & Destructors

 Constant Functions

15/32
Access Control Specifiers
Public
 Can be accessible from anywhere in the program

Private (default)
 Can be accessible only from member functions of its class and
friends

Protected
 Acts as public for objects of its own class and derived classes
 Acts as private to rest of the program

Public functions – Class Interface


Private functions – helper functions (can be accessed by class objects and
friends)
16/32
Constructor and Destructor

Constructor and Destructor are special member functions of class


that are used to construct and destroy the class’s object.

Constructor are called every time you created an object. So


Constructor are initialization function of object.

Destructor are called every time you destroy an object.

17/32
Constructor (Creating an Object)

Three types:

(1) default constructor

(2) constructor with parameters

(3) copy constructor

18/32
How to implement Constructor &Destructor

Constructor Destructor

Function
Same with Class Name ~ with Same Class Name
Name

No return and No need to No return and No need to declare


Return Type
declare return return

It can have different number


Parameter
of parameters and it can No need for parameter
Passing
overload
19/32
Initialization with Default Constructor

class Rectangle Class


{ private:
int width, length;
Object
public:
main()
Rectangle( )
{
{width = 0; Rectangle rect;
length = 0; [Link]();
}
}
void display()
{cout<< width<<endl; Initialization of Object using default
Constructor
cout<<length;
}
Output
}; 0
0
20/32
Initialization with Constructor with Parameter

class Rectangle Class


{
private:
int width, length; Object
public: main()
Rectangle (int w, int l) {
{ width = w; Rectangle rect(3,5);
length = l; [Link]();
} }
void display()
{cout<< width<<endl;
Initialization of Object using
cout<<length;
constructor with parameter
}
};
Output
3
5
21/32
Initialization with Copy Constructor
class Rectangle Class
{
private:
int width, length; Object
public:
Rectangle (int w, int l) int main()
{ { Rectangle r4(60,80);
width = w; length = l; Rectangle r5(r4);
} [Link]();
Rectangle (Rectangle& r) return 0;
{ width = [Link]; }
length = [Link];
} Initialization with Copy Constructor
void display()
{cout<< width<<endl; Output
cout<<length; 60
} 80
};
22/32
Constant Member Functions
Constant Member Function
- Does not modify the state of the object !

 Const function can be invoked from both const & non-const


objects
 Non-const function can be invoked only from non-const object

Declaration
return_type func_name (para_list)const;

Definition
return_type func_name (para_list) const {…}
23/32
Examples of Const Member Function
class Time
{
private :
int hrs, mins, secs ; Function declaration

public :

void Write ( ) const ;


Function definition
};

void Time :: Write( ) const


{
cout <<hrs << “:” << mins << “:” << secs << endl;
}

24/32
Constructor Overloading

A constructor can also be overloaded with more than one


function that have the same name but different types or
number of parameters.

automatically called when an object is created, the one


executed is the one that matches the arguments passed on the
object declaration.

25/32
Example of Const Member Function
#include <iostream> Rectangle::Rectangle (int a, int b) {
using namespace std; width = a;
class Rectangle height = b;
{ }
private:
int width, height; int main () {
public: Rectangle rect (3,4);
Rectangle (); Rectangle rectb;
Rectangle (int, int); cout << "rect area: "<<
int area () [Link]() << endl;
{ cout << "rectb area: "<<
return (width*height); [Link]() << endl;
} return 0;
}; }
Rectangle::Rectangle () {
width = 5;
height = 5;
} What will be the output?  ???
26/32
Interacting Objects
Class A Class B

Private: Private:
data members data members

Member methods Member methods


Public: Public:
Constructor Constructor
Message passing
Destructor Destructor
Other Other
public methods public methods

Private: Private:
methods methods

27/32
Assignment 1

Create a class that contains four member functions, with 0, 1, 2, and 3 int
arguments, respectively. Create a main() that makes an object of that class and calls
each of the member functions.
Now modify the class so it has instead a single member function with all the
arguments defaulted. Does this change the main()?

28/32
Assignment 2
Create a class called time that has separate int member data for hours, minutes,
and seconds.
- One constructor should initialize this data to 0, and another should initialize it to
fixed values.
- Another member function should display it, in [Link] format.
- The final member function should add two objects of type time passed as
arguments.
A main() program should create two initialized time objects and one that isn’t
initialized.
Then it should add the two initialized value together, leaving the result in the third
time variable.
Finally it should display the value of this third variable. Make appropriate member
functions const.
29/32
Assignment 3
Create a class Distance which includes two data members: feet and inches, two
constructors: default constructor and parameter constructor and three member
functions: getdist, showdist and add_dist two distances.
Create three objects in the main: two objects dist1 and dist3 by no argument
constructor and another object dist2 by parameter constructor.
Update the dist1 value by calling member function getdist. Then add two objects
dist1 and dist2 and assign it to dist3.
Show the data in dist1, dist2 and dist3 by calling showdist function.

30/32
Next Lesson

Polymorphism and Operator Overloading

31/32
Thank You!

32/32

You might also like