lec-2
lec-2
lec-2
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)
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.
• #include <iostream.h>
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
int main()
{
calculator <int> object1;
object1.set(100, 200, 300);
cout << object1.add();
}
Encapsulation / Information Hiding/ Data
Hiding
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- 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