lec-2

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Object Oriented

Lecture
Programing
2
Class & Object

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.
The definition of class does not occupy any memory.
For example, int, char, and class do not occupy any memory.

Object
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
};

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.

3-Declaring object of class


Calling member function int main to work on or operate on the object using dot Operator.
Class & 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.
#include<iostream>
using namespace std; Simple class
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(“Bilal Hussain", 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).
Header File

• #include <iostream.h>

• Header file consist of function prototypes and some other


directives

• Function prototypes like:


 int add(int,int);
 int multiply(int,int);
Programing in multiple Files

main.cpp b.h
#include<iostream>
#include “b.h"
using namespace std; • int sum(int a, int b)
•{
// Function Template.
• return a+b;
int main() •}
{
cout<< sum(10,20);

}
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" << s1.getroll();
cout << "\n" << s1.getmarks();
}
Header file : 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 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
Class Template

• A template is a simple yet very powerful tool in C++.


• The simple idea is to pass data type as a parameter so that
we don’t need to write the same code for different data
types
#include<iostream> Template class
using namespace std;
template<class template_name>
class calculator
{
private:
template_name number1;
template_name number2;
template_name number3;
public:
void set(template_name N1, template_name N2,
template_name N3)
{
number1 = N1;
number2 = N2;
number3 = N3;
}
template_name add()
{
return number1 + number2 + number3;
}
};

int main()
{
calculator <int> object1;
object1.set(100, 200, 300);
cout << object1.add();
}
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 a 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.
 Create a class of Circle, Calculate Area of circle using access specifiers
#include<iostream>
using namespace std;
// 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;
}
Class Activity
Private Members Type
Batcode (4 digit), Total_innings, n_out_innings,
Integer
runs, bestscore
batavg float
batname 10 character
Calavg() (Function to compute batsman
float
avegerage )
Public Member:
readdata()
detail: Function to accept value from Batcode,
void
name, innings, not_out and invoke the function
Calcavg()

displaydata()
Detail: Function to display the data members on Void
the screen
Links .

UML Diagram :-

https://www.visual-paradigm.com/guide/uml-unified-modeling-language/uml-class-
diagram-tutorial/https://www.visual-paradigm.com/guide/uml-unified-modeling-
language/uml-class-diagram-tutorial/

Advantages :-

1) https://www.roberthalf.com/blog/salaries-and-skills/4-advantages-of-object-
oriented-programming

You might also like