0% found this document useful (0 votes)
21 views

Lect 9

The document outlines a computer programming course taught by Dr. Ahmed Hamdy Ahmed Arafa at Menoufia University, including course objectives covering basic C++ concepts like functions, macros, arrays, strings, and object-oriented programming. It also discusses programming paradigms like imperative, declarative, structured, and object-oriented programming as well as key concepts in C++ like classes, objects, inheritance, and more. Examples are provided demonstrating how to define classes and initialize/access attributes and methods of objects in C++.

Uploaded by

Mazen Elsherif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lect 9

The document outlines a computer programming course taught by Dr. Ahmed Hamdy Ahmed Arafa at Menoufia University, including course objectives covering basic C++ concepts like functions, macros, arrays, strings, and object-oriented programming. It also discusses programming paradigms like imperative, declarative, structured, and object-oriented programming as well as key concepts in C++ like classes, objects, inheritance, and more. Examples are provided demonstrating how to define classes and initialize/access attributes and methods of objects in C++.

Uploaded by

Mazen Elsherif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Menoufia University

Faculty of Electronic Engineering

CSE 121: Computer Programming

Dr. Ahmed Hamdy Ahmed Arafa


Computer Science & Engineering Dept
Email: [email protected]
Mobile: +201020069729
Office: CSE Building 4th floor, Room 413
Course Objectives

✓ Overview of basic concepts of C++


✓ Functions in C++
✓ Macros in C++
✓ Arrays and Strings in C++
✓ Overview of basic concepts of object oriented programming in C++
✓ Classes and Objects in C++
✓ Inheritance in C++
Programming Paradigms
✓ The style in which a given program or programming language can be organized.
✓ Approach to solve problem using some programming language.
✓ The paradigm consists of certain structures, features, and opinions about how common programming problems should be solved.
✓ It is used to classify programming languages based on their features.

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.

▪ Declarative (What to do)


✓ Focuses on what needs to be done rather how it should be done.
✓ Hides away the complexity and makes programming languages closer to human language and thinking.
✓ The programmer declares properties of the desired result, but not how to compute it (no control flow).
✓ Functional , Logic and Database languages such as (SQL , HTML, CSS) are examples of declarative paradigm languages.
Structured Programming
✓ Programming paradigm that uses structured control flow (without goto statement) to improve code clarity and quality.
✓ It has three ways of combining programs (sequencing, selection, and iteration).
✓ The structured program consists of well structured and separated modules.
✓ The program uses single-entry and single-exit elements.

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.

Types of Object Oriented Programming


▪ Class-based
✓ Classes are defined and objects are instantiated based on these classes
✓ PHP, C++, Java, C# languages are examples.
▪ Prototype-based
✓ No classes exist
✓ Objects are the primary entities
✓ Objects can be created based on already existing objects chosen as their prototype
✓ Java Script is an example.
Classes in C++.
✓ Building block of programs in Class-based Object-Oriented programming.
✓ It is a user-defined data type, which holds its own data members and member functions.
✓ Classes are created before the main function using the following syntax

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> int main() {


// Create The First Car object
using namespace std;
Car carObj1;
class Car carObj1.brand = "BMW";
{ carObj1.model = "X5";
public: carObj1.year = 1999;
string brand;
string model; // Create The Second Car object
Car carObj2;
int year;
carObj2.brand = "Ford";
}; carObj2.model = "Mustang";
carObj2.year = 1969;

// Print attribute values


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 3: Initializing its attributes in the main function
Create a student class that have 5 data members for the student which are id, name and three degrees and then create two methods in the class finding
the average degrees and deciding the student grade. Create and object in the main program. All class attributes are initialized directly with declaration.

#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;
}

You might also like