Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

OBJECT ORIENTED PROGRAMMING (OOP) WITH C++

CLASS AND OBJECTS


AIUB. SPRING 2018

Dr. Mahbubul Syeed


Associate Professor, Department. of CS, AIUB
mahbubul.syeed@aiub.edu
www.msyeed.weebly.com
CONTENTS

 Class and Object definition and declaration


 Class data member (attributes) and
 Class member function (methods).
 Accessing class data member and member function from inside and outside of the class,
 Access modifiers:
 For class.
 For member variables / member data (attributes)
 For member functions (methods).
CLASS AND OBJECTS

Class: A class is a template definition consisting of attributes (or variables) and methods (or
functions) to present a particular type of objects.

Attributes / variables: Describe the properties that the objects should have.

Methods / functions: Describes or defines the actions / functions that those group of objects
should perform

Object: Objects are instances of a class.


HOW TO DECLARE A CLASS IN C++

class class_name class Person {


{ private:
string firstName;
// list of member variables / attributes
string lastName;
access_modifier: double age;
member1;
member2; public:
void setFirstName(string name);
// list of member functions / methods void setLastName(string name);
access_modifier: void setAge(double age);
memberfunction_1(arguments..);
memberfunction_2(); string setFirstName();
string getLastName();
}; double getAge();
};
HOW TO DECLARE OBJECT AND ACCESS MEMBERS

Declare objects of a class

class_name objectname1, objectname2;

Accessing member functions of a class

object1.memberfunction_1(arguments);
Name of the object followed by a dot (.) operator
object2.memberfunction_1(arguments); followed by the function name and arguments
(if any)
object1.memberfunction_2();
object2.memberfunction_2();
Declare the class Define the member functions
#include <iostream> void Person::setName(string nm){
using namespace std; name = nm;
}
class Person {
void Person::setAge(double ag){
private: age = ag;
string name; }
double age;
string Person::getName(){
public: return name;
void setName(string nm); }
void setAge(double ag);
double Person::getAge(){
string getName(); return age;
double getAge(); }
};
Create objects of the class and access the member variables and functions

int main()
{
// Creating objects of person class
Person p1;

// accessing member functions


p1.setName("Abdul Jobber");
p1.setAge(40);

Person p2;
p2.setName("Birbal");
p2.setAge(65);

cout << "P1 name: " << p1.getName() << endl << " P1 age: " << p1.getAge() << endl;
cout << "P2 name: " << p2.getName() << endl << " P2 age: " << p2.getAge();
}
ACCESS MODIFIERS – FOR MEMBER VARIABLES AND FUNCTIONS

Access modifier name Description Detail


Private Can only be accessed within the a. Only be accessed by the member
class. functions of the class.
b. By default the member variables of a
class are private.
Public Can be accessible by any one. a. Accessible within the class.
b. Accessible from any part of the program
using an object followed by a dot (.)
operator.
Protected Similar to Private. a. Useful in creating Inheritance (is-a)
In addition it can be accessed by relationship.
derived class.
ACCESS MODIFIERS – FOR CLASSES

Access modifiers for classes are used to implement Inheritance in C++.

More detail on this topic will be provided in Inheritance lecture.


Private access modifier (Example)
#include <iostream>
using namespace std;
int main()
class Person {
{
private:
// Creating objects of person class
string name;
Person p1;
public:
void setName(string nm);
// accessing member functions (Because they are public)
string getName();
p1.setName("Abdul Jobber");
};
cout << "P1 name: " << p1.getName();

//not allowed (as they are privatre)


void Person::setName(string nm){
p1.name = "Mostafa Jobber";
name = nm;
cout << "P1 name: " << p1.name;
}
}
string Person::getName(){
return name; In function 'int main()': 7:13: error: 'std::string
} Person::name' is private 31:6: error: within this
context
Public access modifier (Example)
#include <iostream>
using namespace std;
int main()
class Person {
{
public:
// Creating objects of person class
string name;
Person p1;
public:
void setName(string nm);
// accessing member functions (Because they are public)
string getName();
p1.setName("Abdul Jobber");
};
cout << "P1 name: " << p1.getName() << endl;

//its allowed to access public variables


void Person::setName(string nm){
p1.name = "Mostafa Jobber";
name = nm;
cout << "P1 name: " << p1.name;
}
}
string Person::getName(){
return name;
} P1 name: Abdul Jobber
P1 name: Mostafa Jobber
By default all members are private
#include <iostream>
using namespace std;
int main()
class Person {
{
string name;
// Creating objects of person class
void setName(string nm);
Person p1;
string getName();
};
// accessing member functions
p1.setName("Abdul Jobber");
cout << "P1 name: " << p1.getName();
void Person::setName(string nm){
name = nm;
//not allowed (as they are privatre)
}
p1.name = "Mostafa Jobber";
cout << "P1 name: " << p1.name;
string Person::getName(){
}
return name;
}
Neither member variable nor member functions
can be accessed from outside the class!!! As they
are all private now!!!
MEMORY ALLOCATION

 For each instance (i.e., object) of the class, memory is allocated to only its
member variables.

 Each instance of the class doesn't get it's own copy of the member function. All
instances share the same member function code.
What will be the output of the following program?
- Output and Error Tracing

#include <iostream> int main()


using namespace std; {
Person p1;
class Person {
void Person::setName(string nm){ p1.setName("Abdul Jobber");
name = nm; p1.setAge("30");
string name;
} p1.dob = "1.1.1900";
public:
string Person::setAge(){
string dob;
age = ag; cout << p1.getName() << endl << p1.getAge() << p1.dob;
private:
}
double age;
string Person::getName(){ p1.age = "55";
double getAge();
return name; cout << "New Age is: " << p1.getAge();
}
public:
string Person::getAge(){ p1.dob = "5.5.1995";
void setName(string nm);
return age; cout << "New DOB is: " << p1.dob;
void setAge(double ag);
}
string getName(); p1.name = "Mostafa";
cout << "New Name: " << p1.getName();
}; }
 EXERCISE:

WRITE A CLASS NAME STUDENT.

CREATE TWO PRIVATE VARIABLES TO STORE STUDENT ID, AND NAME.

WRITE TWO MEMBER FUNCTIONS TO STORE STUDENT ID AND NAME.

WRITE ANOTHER TWO MEMBER FUNCTIONS TO RETRIEVE THE STUDENT ID AND NAME.

FINALLY CREATE THREE STUDENT OBJECTS TO STORE AND PRINT THEIR DATA IN MAIN FUNCTION.
#include <iostream>
#include<string>
using namespace std;

class Student {
int studentId;
string studentName;

public: int main () {


void set_student_info (int,string); Student student1, student2, student3;
void print_student_info();
}; student1.set_student_info (123, "Mosaddek");
student2.set_student_info (456, "Mojaffar");
void Student::set_student_info (int id, string name) { student3.set_student_info (789, "Salauddin");
studentId = id;
studentName = name; student1.print_student_info();
} student2.print_student_info();
student3.print_student_info();
void Student::print_student_info() {
cout << "id: " << studentId; return 0;
cout << "\tName: " << studentName << endl << endl;
} }
ASSIGNMENT 2

Create a class called Rectangle. A rectangle should have height and width to describe it. Write
appropriate methods to set and get the height and width of a rectangle. Additionally, write a method
to calculate area of a rectangle.

Now create two rectangle objects in the main function. Set their height and width. Then print the area
of the rectangle which has larger area between the two.

Submission detail:
- Email attachment (.txt file)
- Email Subject: assignment2_PL2
- Write your name, id and section in the email body

- Submit your assignment to: fahmida@aiub.edu


- Deadline: 11.4.2018 (12:00 PM) HARD DEADLINE

You might also like