Lect 9
Lect 9
Paradigms
Imperative Declarative
Object Parallel
Structured Logic Functional Database
Oriented Processing
Imperative Paradigm VS Declarative Paradigm
▪ Imperative (How to do)
✓ Focuses on how the problem should be solved by describing solving steps , step by step which requires understanding of
the functions necessary to solve the problem.
✓ It's called imperative because the programmer instructs exactly what the computer has to do, in a very specific way.
✓ It features close relation to machine architecture
✓ The paradigm consist of several statements and after execution of all the result is stored.
✓ Imperative programming consists of sets of detailed instructions that are given to the computer to execute in a given order.
✓ Structured and object-oriented programming (OOP) languages fall under imperative programming, such as C, C++, C#,
and Java.
Advantages
• The code is well written and organized.
• Easier to maintain and debug.
• We can execute a block of code repeatedly till the given conditions match.
• Improved decision making power by using some conditions that can decide to execute or not any block of code.
• The structured flow of execution using if/else and for/while.
• Mainly problem based instead of being machine based.
• Development is easier and requires less effort and time.
Disadvantages
• The program depends upon changeable factors like data-types, therefore it needs to be updated with the need on the go.
• Usually the development in this approach takes longer time as it is language-dependent.
Object Oriented Programming (OOP)
✓ OOP mainly focuses on objects that are required to be manipulated.
✓ OOP combines both data and functions into a single unit called object.
✓ The object is a real-world entity that has data (properties/variables) and behavior (methods / Functions).
✓ The basic unit in OOP is the class which is the template / blueprint for creating objects which are instances created from the class.
Advantages of OOP
✓ Data security though data hiding
✓ Encapsulation
✓ Abstraction
✓ Productivity through many libraries that new programs requires.
✓ Inheritance
✓ Code reusability
✓ Flexibility through polymorphism.
Data Members
✓ Attributes /features/variables used to store the data in the memory.
✓ Optional in the class.
✓ Data members Initialization
▪ Directly with declaration in the class
▪ Directly with assignment using object in the main function
▪ Creating specific method for initializing the variables
▪ Using Constructors
Access Modifiers / Specifiers
✓ It means how class members (attributes / methods) can be accessed
▪ Public: members are accessible from outside the class
▪ Private: members cannot be accessed (or viewed) outside the class
▪ Protected: members can be accessed in inherited classes
▪ By default all members are private if no specifier is determined
class ABC {
int x; // Private attribute
int y; // Private attribute
};
Class Methods / Functions in C++.
▪ Member functions in the class are used to process and manipulate the data members.
▪ It represents the behavior of the object.
▪ It is as normal functions in C++ ( Declaration outside the main , Definition outside the main , Call inside the main program).
▪ It can be void or has a return type
▪ It is called inside another function in the same class or other classes (Considering access modifiers) or inside the main program
▪ Declaration and definition of class methods
✓ Combine Definition and Declaration inside the class
class MyClass
{
public:
void myMethod()
{ // Method defined inside the class
cout << "Hello World!";
}
};
✓ Declaration inside the class and Definition Outside the class
class MyClass
{
public:
void myMethod(); // Method/function declaration
};
void MyClass::myMethod() // Method definition outside the class
{
cout << "Hello World!";
}
Objects in C++.
✓ Objects are instances from the class.
✓ Used to access both data members and methods.
✓ Objects are declared inside the main program → ClassName ObjectName;
✓ Member Access operator dot (.) is used to access a member in the object (Function/Attribute)
int main()
{
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method using the object
myObj.attribute1 = "ABC"; // Access the attribute using the object
return 0;
}
Example 1: Initializing the attributes in the class
Create A class named Car that has three attributes which are the car brand , model and year of production. Create two objects of two different cars
and print them on the screen.
#include<iostream>
using namespace std;
class Car
{
public:
string brand="BMW";
string model=“X5";
int year=1999;
};
int main() {
Car carObj1 ;
Car carObj2;
carObj2.brand = "Ford";
carObj2.model = "Mustang";
carObj2.year = 1969;
cout <<"The First Car is:"<<carObj1.brand<<" "<< carObj1.model<<" "<<carObj1.year<< "\n";
cout <<"The Second Car is:"<<carObj2.brand<<" "<< carObj2.model<<" "<<carObj2.year<< "\n";
return 0;
}
Example 2: Initializing attributes in the main function
Create A class named Car that has three attributes which are the car brand , model and year of production. Create two objects of two different cars
and print them on the screen.
#include <iostream>
using namespace std;
class Student{
public:
int id;
string name;
float D1;
float D2;
float D3;
public:
float get_degree() {return ((D1+D2+D3)/3 );}
string get_grade( float degree)
{
if (degree<50) return "Fail";
else if(degree>=50 && degree<65) return "Pass";
else if(degree>=65 && degree<75) return "Good";
else if(degree>=75 && degree<85) return "Very
Good";
else return "Excellent";
}
};
Example 3: Initializing its attributes in the main function (cont.).
int main()
{
int sid;
string sname;
float sd1,sd2,sd3;
cout<<"Please Enter the Student ID:"<<endl;
cin>>sid;
cin.ignore();
cout<<"Please Enter the Student Name:"<<endl;
getline(cin,sname);
cout<<"Please Enter the Student First Degree:"<<endl;
cin>>sd1;
cout<<"Please Enter the Student Second Degree:"<<endl;
cin>>sd2;
cout<<"Please Enter the Student Third Degree:"<<endl;
cin>>sd3;
Student s1;
s1.id=sid;
s1.name=sname;
s1.D1=sd1;
s1.D2=sd2;
s1.D3=sd3;
cout<<"The Student Information is:\t id \tname \tD2 \tD2 \tD3"<<endl;
cout<<"The Student Information is:\t "<<s1.id<<"\t"<<s1.name<<"\t"<<s1.D1<<"\t"<<s1.D2<<"\t"<<s1.D3<<endl;
cout<<"\nThe Average Degree is:"<<s1.get_degree()<<endl;
cout<<"\nThe Student Grade is:"<<s1.get_grade(s1.get_degree())<<endl;
return 0;
}