Programming Concepts Instructor: Ms. Zainab Iftikhar Arrays • Write a C++ program to find minimum value from an array of 5 members. • Syntax: int a[5]; Solution #include <iostream> using namespace std; int main(){ int input[4], i, min; for(i = 0; i <= 4; i++){ cin >> input[i]; } min = input[0]; for(i = 0; i <= 4; i++){ if(input[i] < min){ min = input[i]; } } cout << "Minimum Element\n" << min; } Dry Run 2d Array Output Using a Pointer Variable x; 2000 x = 12; 12 int x int* ptr; ptr = &x; 3000 2000 ptr NOTE: Because ptr holds the address of x, we say that ptr “points to” x 5 Types of Functions Functions • Write a C++ program to write a function named sum. Pass two integer type values from main to function and return the sum of numbers in main • Function Declaration • Function Definition • Function Call Solution #include <iostream> using namespace std; int sum(int a, int b); int main() { int x,y,z; cout<<"Enter value for x: "; cin>>x; cout<<"Enter value for y: "; cin>>y; z = sum(x,y); cout<<"Sum "<<z<<endl; } int sum(int a,int b) { int c; c= a+b; return c; } Object Oriented Programming Techniques Programming Paradigm Procedural Programming • Structured/Modular Programming Object Oriented Programming History Difficult to code using Machine Language After the invention of the microprocessor, assembly languages flourished • Importance to functions rather than data • Functions and Modules • • • • Functions Used for medium-sized applications Data is global Security issues History • These programming constructs were developed in the late 1970s and 1980s • Issues with well structured programs • Over generalization • Correlation problem • Not suitable for real time applications Solution: Object Oriented Approach Object Oriented Approach • Revolution in the programming methodology field • Generalization • Real-time objects creation • Results in more organized, reusable, and maintainable code • Languages: • C++ (C with Classes) • Java • Python • C# Object Oriented Programming • OOP is centered around the object, which packages together both the data and the functions that operate on the data • Object-oriented programming (OOP) is a programming language model organized around objects rather than "actions" and data rather than logic OOP Terminology • In OOP, an object’s member variables are often called its attributes and its member functions are sometimes referred to as its behaviors or methods