100% found this document useful (1 vote)
211 views14 pages

OOP Notes

The document provides an introduction to classes in C++. It defines a class as consisting of both data and methods that define properties and behavior of a set of entities. An object is an instance of a class. The document discusses class declaration syntax, access specifiers like public and private, creating objects, member functions, and provides examples of a simple class with data members and methods. It also presents an exercise to write a class to calculate the area of a rectangle by taking length and width as inputs and displaying the output.

Uploaded by

Saim Chaudhary
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
100% found this document useful (1 vote)
211 views14 pages

OOP Notes

The document provides an introduction to classes in C++. It defines a class as consisting of both data and methods that define properties and behavior of a set of entities. An object is an instance of a class. The document discusses class declaration syntax, access specifiers like public and private, creating objects, member functions, and provides examples of a simple class with data members and methods. It also presents an exercise to write a class to calculate the area of a rectangle by taking length and width as inputs and displaying the output.

Uploaded by

Saim Chaudhary
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
You are on page 1/ 14

Object-Oriented Programming

Lecture 5A – Introduction to Classes

Ubaid Ur Rehman
[email protected]
Outline
™  Classes

™  Objects
Classes and Objects
™  The class is the cornerstone of C++
™  It gives the C++ its identity from C
™  It makes possible encapsulation, data hiding and
inheritance

™  Class:
™  Consists of both data and methods
™  Defines properties and behavior of a set of entities

™  Object:
™  An instance of a class
™  A variable identified by a unique name
Declaring a Class
Header class Rectangle
class class_name {
{ private:
permission_label: int width;
member;
Body
int length;
permission_label:
public:
member;
void set(int w, int l);
...
int area();
};
};
Class: Access Specifiers
™  Information hiding
™  To prevent the internal representation from direct
access from outside the class
™  Access Specifiers
™  public: May be accessible from anywhere within a
program
™  private: May be accessed only by the member
functions, and friends of this class, not open for
nonmember functions
™  protected: Acts as public for derived classes (virtual).
While, behaves as private for the rest of the program
Class: Access Specifiers
™  Within the body, the keywords private: and public:
specify the access-level of the members of the class.
™  the default is private.
Creating Objects
class Rectangle
Rectangle r1;
{ Rectangle r2;
private: Rectangle r3;
int width;

……
int length;
public:
void set(int w, int l);
int area(); int a;
}
Class: Member Functions
™  Member functions are functions that are included
within a class.
™  Also called methods
class rectangle
{
private: No need for other classes to access
and retrieve its value directly. The
int width; class methods are responsible for
int length; that only.
public: They are accessible from outside
void set(int w, int l) the class, and they can access the
{ cout<<l*w; } members width and length.
};
Example
// This program declare a class void out()
with one integer data member and {
two member functions
cout<<"The value of n= "<<n;
#include <iostream>
using namespace std; }
class Test };
{ int main()
private: {
int n; Test obj;
public:
obj.in();
void in()
obj.out();
{
cout<<"Enter a number "; }
cin>>n;
}
Example: Output

Enter a number 10
The value of n= 10
Exercise
// Book with three members cout<< "Pages: "<<Pages<<endl;
#include <iostream> cout<< "Price: "<<Price<<endl; }
using namespace std; void set(int id, int pg, float pr)
class Book { { BookID = id;
private: Pages = pg;
int BookID, Pages; Price = pr; }
float Price; int getPrice()
public: { return Price; }};
void get() int main(){ Book b1, b2;
{ cout<< "Enter Book ID: "; b1.get();
cin>>BookID; b2.set(2, 320, 150.7);
cout<< "Enter Pages: "; cout<< "\n The detail of most
cin>>Pages; costly book is as follows: "<<endl;
cout<< "Enter Price: "; if(b1.getPrice()>b2.getPrice())
cin>>Price; } b1.show();
void show() else
{ cout<< "Book ID: b2.show();
"<<BookID<<endl; return 0; }
Exercise: Output

Enter Book ID: 1


Enter Pages: 250
Enter Price: 125.50

The detail of most costly book is as follows:


Book ID: 2
Pages: 320
Price: 150.7
Class Exercise
™  Write a program using objects/classes that
calculates the area of a rectangle and displays it on
the screen. HINT: area = length * width.
Class Exercise Solution
#include <iostream> int main()
using namespace std; {
class rect_area
rect_area a;
{
private:
int area; a.get_area(5, 3);

public:
a.show_area();
void get_area(int l, int w)
{ return 0;
area = l * w; }
}
void show_area()
{ Output:
cout << "Area of rect. is " << Area of rect. is 15
area<<endl;
}
};

You might also like