Professional Documents
Culture Documents
Lect 9-10
Lect 9-10
Lect 9-10
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;
}
Example 4: Initializing the attributes using a method
Create a student class that have 5 data members for the student which are id, name and three degrees and then create three methods in the class for
initializing the class attributes, finding the average degrees and deciding the student grade. Create and object in the main program.
✓ To avoid this type of this duplication and make the methods code reusable , inheritance is used.
✓ We can easily create a class Vehicle and write these three functions in it and inherit the rest of the classes from the vehicle class.
Inheritance in C++ (cont.).
Inheritance Properties and Implementation
✓ Inheritance implements the IS-A relationship ( Rectangle is a Figure)
✓ The new class is called Derived / Child / Sub class and the existing class is known as the Base / Parent / Super class
✓ Sub / Derived / Child class is the class that inherits properties from another class.
✓ Super / Super class is the class whose properties are inherited.
✓ The derived class inherits all the properties (Attributes and Methods) of the base class, without changing the properties of base
class and may add new features to its own but we must consider the access specifiers of the class.
✓ These new features in the derived class will not affect the base class.
✓ A derived class doesn’t inherit access to private data members.
✓ When a base class is privately inherited by the derived class, public members of the base class becomes the private members of
the derived class and therefore, the public members of the base class can only be accessed by the member functions of the derived
class. They are inaccessible to the objects of the derived class.
✓ On the other hand, when the base class is publicly inherited by the derived class, public members of the base class also become
the public members of the derived class. Therefore, the public members of the base class are accessible by the objects of the
derived class as well as by the member functions of the derived class.
✓ For creating a sub-class that is inherited from the base class we have to follow the below syntax.
class <derived_class_name> : <visibility mode> <base_class_name>
{ //body }
}; };
};
class B: protected A class B: private A
class B: public A
{ {
{
// x is protected // x is private
// x is public
// y is protected // y is private
// y is protected
// z is not accessible from C // z is not accessible from D
// z is not accessible from B
} }
}
Types of Inheritance in C++ (cont.).
Single inheritance Multilevel inheritance Hierarchical inheritance
✓ Derived class is inherited from only one base ✓ Deriving a class from another derived class. ✓ Deriving more than one class from a base class.
class that is not inherited from any other classes. class A
class A
{
class A {
};
{ }
class B: public A
}; class B : public A
{
class B: public {
};
A }
class C: public B
{ class C : public A
{
} {
};
}
class D : public A
{
}
Multiple inheritance Hybrid inheritance
✓ Deriving a new class that inherits from two or more classes. ✓ Combination of more than one type of inheritance.
class A class A
{ {
}; }
class B class B : public A
{ {
}; }
class C: public A, public B class C : public A
{ {
}; }
class D : public B , public C
{
}
Inheritance Example.
#include<iostream> class Triangle : public Shape
using namespace std; {
class Shape public:
{ Triangle (float x, float y): Shape(x,y)
public: {
float a; }
float b; float triangle_area()
public: {
Shape(float n,float m) return (0.5*a*b);
{ }
a=n; };
b=m; int main()
} {
};
class Rectangle : public Shape int l,w,b,h;
{ cout << "Enter the length and width of a rectangle:" <<endl;
public: cin>>l>>w;
Rectangle (float x, float y): Shape(x,y) Rectangle r(l,w);
{ cout << "Area of the rectangle is:" <<r.rectangle_area()<<endl;
} cout << "Enter the base and height of the triangle:" <<endl;
int rectangle_area() cin>>b>>h;
{ Triangle t (b,h);
return (a*b); cout <<"Area of the triangle is:" <<t.triangle_area()<<endl;
} return 0;
}; }
Any Questions?????