0% found this document useful (0 votes)
27 views25 pages

Lecture 2

Uploaded by

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

Lecture 2

Uploaded by

hamzaazizug98
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

CS-2203 Object Oriented

Programming
Class
In programming
A class is like a user-defined data type.
A class is a collection of data and functions, the data items and
functions are defined within the class.

Class and Memory


The definition of class does not occupy any memory.
For example, int, char, and class do not occupy any memory.
Objects
In programming
An object is variable of class type from which it is declared / created.
Each object of class having unique name (follow rules of declaring variable)

As we know an object have characteristics/properties/attributes and


behaviors/actions, in C++ or any object oriented programming language the
properties of object represented by data members( the built in data types)
and behaviors/action by the member functions.
Class structure
Defining a class
class class_name
{
body of class/definition of class
};

body of class/definition of class

1- Data members
(Data items used to represent attributes/characteristics/properties of an object)

2-Member functions/methods
Functions used to work on its data members are called member functions or methods.

Declaring object of class


Calling member function to work on or operate on the object using dot
Operator.
Class vs. Objects
Class vs. Objects
Why classes are required

It’s about being able to write more efficient and reusable


It is a lot easier, quicker, and more securer to pull out the
information from a class.
Write simple class
•#include<iostream>
•using namespace std;
class student{
• private:
• string name;
• int id;
• public:
• set(string N, int ID){
• name = N;
• id = ID; }
string getname(){
• return name;}
• int getid(){
• return id;}
void display(){
• cout<< getname();
• cout<<endl;
• cout<< getid();
• }};
•int main()
•{
• student s1;
• s1.set("Umer Arshad Butt", 51);
• s1.display();
•}
ACCESS SPECIFIERS

In C++, there are three access specifiers:


•public - members are accessible from outside the class
•private - members cannot be accessed (or viewed) from
outside the class
•protected - members cannot be accessed from outside the
class, however, they can be accessed in inherited classes.
(In Inheritance we will cover it in detail next
lectures).
Working in different files (separate interface and
implementation)
file name: main.cpp
•#include<iostream>
•#include "import.h"

using namespace std;

int main()
•{
• student s1;
• s1.setname("Umer Arshad Butt");
• s1.setroll(51);
• s1.setmark(100);

cout<<s1.getname();
• cout<<"\n";

cout<<s1.getroll();
• cout<<"\n";

cout<<s1.getmarks();
•}
Working in different files (separate interface and
implementation)
import header file name: import.h
•using namespace std;

class student{
• private:
• string student_name;
• int roll_no;
• float marks;

public:
• void setname(string N)
• {
• student_name = N;
• }

void setroll(int R)
• {
• roll_no = R;
• }

void setmark(float M)
• {
• marks = M;
• }

string getname()
• {
• return student_name;
• }

int getroll()
• {
• return roll_no;
• }

int getmarks()
• {
• return marks;
• }
•};
Class Activity
Private Members Type
Batcode (4 digit), Total_innings, Integer
n_out_innings, runs, bestscore

batavg float
batname 10 character
Calavg() (Function to compute float
batsman avegerage )
Public Member:
readdata() void
detail: Function to accept
value from Batcode, name,
innings, not_out and invoke the
function Calcavg()

displaydata() Void
Detail: Function to display the
data members on the screen
Class UML Diagram
Difference between classes
and structures
• Technically speaking, structs and classes are almost
equivalent
• The major difference like class provides the flexibility of
combining data and methods (functions ) and it provides
the re-usability called inheritance.
• A class has all members private by default. A struct is a
class where members are public by default
Difference between classes
and structures
• // Program 1
• #include <stdio.h>

• struct Test {
• int x; // x is public
• };
• int main()
• {
• Test t;
• t.x = 20; // works fine because x is public getchar();
• return 0;
• }
Difference between classes
and structures
• // Program 2
• #include <stdio.h>

• class Test {
• int x; // x is private
• };
• int main()
• {
• Test t;
• t.x = 20; // compiler error because x is private
• getchar(); return 0;
• }
Encapsulation /
Information Hiding/ Data
Hiding
• Information hiding is one of the most important principles of OOP
inspired from real life which says that all information should not be
accessible to all persons.

• The Private information should only be accessible to its owner (object


of particular class).

• Theoretically Information hiding we mean


• “Showing only those details to the outside world which are
necessary for the outside world and hiding all other details
from outside world”.
Advantages of Information
Hiding
1) The advantage of data encapsulation comes when the
implementation of the class changes but the interface remains the
same.

2) It is used to reduce the human errors. The data and function are
bundled inside the class that take total control of maintenance and thus
human errors are reduced.

3) Makes maintenance of application easier.

4) Improves the understandability of the application.

5) Thus the concept of encapsulation shows that a non member function


cannot access an object’s private or protected data which provide
Security to data.
Real Life Example of
information Hiding
1- Your Name and your personal information is stored in your brain, we
can not access this information directly. In order to ask your name and
your personal information we need to ask you about your details and it
will be up to you how much information you want to share with us.

2- No one can access the information/ contacts of your cell phone if you
locked memory of your cell phone with some password.

3- While you login to your email, you must need to provide the hidden
information (Password) to your server.
Encapsulation

The meaning of Encapsulation, is to make sure that "sensitive" data


is hidden from users. To achieve this, you must declare class
variables/attributes as private (cannot be accessed from outside the
class).If you want other to read or modify the value of a private
member, you can provide public get and set methods.
Encapsulation
• #include <iostream>
using namespace std;

class Employee {
private:
// Private attribute
int salary;

public:
// Setter
void setSalary(int s) {
salary = s;
}
// Getter
int getSalary() {
return salary;
}
};

int main() {
Employee myObj;
myObj.setSalary(50000);
cout << myObj.getSalary();
return 0;
Class Activity

1. Create a class for a Cylinder and create a function to calculate


the volume of the cylinder. However, encapsulate the functions,
and be sure to protect the variable for volume from the outside
world
2. Create a class of Circle. Calculate the Area of the circle using
access specifiers
Class Activity
Private Members Type
Batcode (4 digit), Total_innings, Integer
n_out_innings, runs, bestscore
batavg float
batname 10 character
Calavg() (Function to compute batsman float
avegerage )
Public Member:
readdata() void
detail: Function to accept value from
Batcode, name, innings, not_out and
invoke the function Calcavg()
displaydata() Void
Detail: Function to display the data
members on the screen
Class Activity
// C++ program to demonstrate public
// access modifier

#include<iostream>
using namespace std; Class Activity
// class definition
class Circle
{
public:
double radius;

double compute_area()
{
return 3.14*radius*radius;
}

};

// main function
int main()
{
Circle obj;

// accessing public datamember outside class


obj.radius = 5.5;

cout << "Radius is: " << obj.radius << "\n";


cout << "Area is: " << obj.compute_area();
return 0;
}
Output:
Radius is: 5.5
Area is: 94.985

You might also like