Oop Lab As1
Oop Lab As1
OBJECT ORIENTED
PROGRAMMING-LAB
By
(CMS-ID):2382-2022
Program BSCS-3A
Course Instructor
SIR UZAIR
PROCEDURAL PROGRAMMING
#include <iostream>
#include <string>
int main() {
std::string name;
int age;
getName(name);
getAge(age);
displayDetails(name, age);
return 0;
}
Vs
OBJECT ORIENTED PROGRAMMING
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
void getName() {
std::cout << "Enter name: ";
std::cin >> name;
}
void getAge() {
std::cout << "Enter age: ";
std::cin >> age;
}
void displayDetails() {
std::cout << "Name: " << name << std::endl;
std::cout << "Age: " << age << std::endl;
}
};
int main() {
Person person;
person.getName();
person.getAge();
person.displayDetails();
return 0;
}
Comparison:
Procedural (C-style) code example focuses on a linear flow of control using functions. Each function has
a specific purpose and the flow of control moves from one function to another. This style of coding can
lead to better organization in certain scenarios.
Object-Oriented Programming (OOP) example, on the other hand, uses a class to define an object with
attributes (name and age) and methods (getName, getAge, and displayDetails). This style of coding
provides better encapsulation, allowing the object
Methods: Methods are functions defined within a class that represent the behavior or actions that
objects of that class can perform.
Access Modifier: Access modifiers in OOP control the visibility and accessibility of class members.
Common access modifiers include public (accessible from anywhere), private (only accessible within the
class), and protected (accessible within the class and its subclasses).
Structure:
In C and C++, a structure is a composite data type that allows you to group variables of different data
types under a single name. It’s used to create a user-defined data type that can represent a record or
data structure with various fields, each of which can have its own data type. Structures are often used to
encapsulate related data, making it easier to organize and work with complex data. The fields (also
known as members) of a structure are accessed using the dot (.) operator.
Array:
An array is a data structure in C and C++ that stores a fixed-size, sequential collection of elements of the
same data type. Arrays provide efficient data access because elements are stored in contiguous memory
locations, allowing for fast indexing. However, arrays have a fixed size, which means you must specify
the size when declaring them, and this size cannot be changed dynamically. Arrays are commonly used
for tasks that involve storing and processing a collection of similar data, such as a list of numbers or
strings.