Technological University (Hmawbi)
Department of Electronic Engineering
EcE – 42024
Computer Science II
Daw ThetThet Mar
Lecturer
EC Department
1
Chapter 2
Objects and Classes
• Before you create an object in C++, you need to
define a class.
• A class is a blueprint for the objects.
• A powerful feature of C++ is that we can create our
own data types called classes.
• A class is a user defined data-type which has data
members and member functions.
• An object is an instance of a class. When a class is
defined, no memory is allocated but it is instantiated
(i.e: an object is created) memory is allocated.
2
How to define a Class in C++
• A class definition begins with the keyword class
followed by the name of class.
• The body of the class is contained within a set of
braces, { } ; (notice the semi-colon).
class class_name
{
…. Any valid identifier
….
….
Class body (data
}; member + functions)
3
Access Specifiers
• Access specifiers define how the members (attributes and methods) of a class
can be accessed.
• There are three access specifiers:
public – members are accessible from outside the class
private – members cannot be accessed from outside the class
-- only access from inside the same class (default)
protected – members cannot be accessed from outside the
class, however, they can be accessed in inherited
classes.
4
• Classes in C++
class class_name
{
private: private members or
…
…
methods
…
public: public members or
…
…
methods
…
};
5
fig: syntax of a class definition
6
fig: private and public
7
C++ Objects
• To use the data and access function defined in the class, you need to
create objects.
• Syntax to define objects in C++ :
classname objectvariablename ;
• We can create many objects from a class.
How to access data members and members
functions in C++
• You can access the member datas and member functions by using a .
(dot ) operator / (member access operator).
8
Eg 1 Create a class is called smallobj. The only one data item is the type
int for somedata. A member function called getdata( ) gets the data from
the user and another function called setdata( ) initialize to fixed value. The
function called showdata( ) displays the result. The main program
initializes two data. One is in fixed value and the other is from the user.
Then show them.
9
Eg 1 //demonstrates a small, simple object
// smallobj.cpp void showdata() {
#include <iostream> cout << "Data is : " << somedata << endl;
using namespace std; }
class smallobj { //define class };
private: int main(){
int somedata; //data member smallobj s1, s2;
public: s1.getdata(); //calling member function
void getdata( ) { //member function s2.setdata(1066);
s1.showdata();
cout<<“Enter some data: ” ;
s2.showdata();
cin>>somedata;
return 0;
}
}
void setdata(int d) //member function
{ Output of the Program:
somedata = d; Data is :
} Data is : 1066
10
Eg 2 /* C++ objects as physical void showpart()
objects */ {
cout << "Model: " << modelnumber <<
#include <iostream>
endl;
using namespace std; cout << "Part: " << partnumber <<
class part { endl;
cout << "Cost: " << cost << endl;
private: }
int modelnumber; };
int partnumber; int main()
{
float cost; part part1,part2;
public: part1.setpart(555,111,217.35);
part2.setpart(666,222,580.72);
void setpart(int mn, int pn, float c)
part1.showpart();
{ part2.showpart();
modelnumber = mn; return 0;
}
partnumber = pn;
cost = c;
}
11
Output of the Program:
Model: 555
Part: 111
Cost: 217.35
Model: 666
Part: 222
Cost: 580.72
12
Eg 3 //C++ objects as data types cout<< "Enter Inches: ";
using English measurements cin>> inches;
#include<iostream>
}
using namespace std;
void showdist()
class Distance
{
{
private: cout << feet << "\‘- "<<inches <<"\""<<endl;
int feet; }
float inches; };
public: int main()
void setdist(int ft, float in) {
{
Distance dist1,dist2;
feet = ft;
dist1.setdist(11,6.25);
inches = in;
dist2.getdist();
}
void getdist() cout<<“\ndist1=”; dist1.showdist();
{ cout<<“\ndist2=”;dist2.showdist();
cout << "Enter Feet: " ; return 0;
cin >> feet; } 13