460 likes | 657 Views
104 CS Computer Programming II . Mujthaba Gulam Muqeeth Lecturer, Salman Bin Abdulaziz University Department of Computer Science & information College of Arts & Science Wadiwadassir. Week 1 . Introduction to Object Oriented Programming Procedure oriented programming Languages
E N D
104 CS Computer Programming II MujthabaGulamMuqeeth Lecturer, Salman Bin Abdulaziz University Department of Computer Science & information College of Arts & Science Wadiwadassir
Week 1 • Introduction to Object Oriented Programming • Procedure oriented programming Languages • Object Oriented Programming languages • Differences.
Procedure Oriented Programming Languages • Emphasis is on doing things (algorithms). • Large programs are divided into smaller programs known as functions. • Most of the functions share global data. • Data move openly around the system from function to function. • Functions transform data from one form to another. • Employs top-down approach in program design.
Procedure Oriented Programming Languages (Cond..) • Disadvantages of PoP • The data can be accessed by any function no security. • It does not support the concepts of Object oriented programming language. • Differences between structures and classes • The structures are used to hold only data in c. Where as the classes used to hold data and ass well as functions. • In class the data can be freely flow from one function to another function. • If we want to call the data of any class we need to call the functions first
Object Oriented Paradigm • Emphasis is on data rather than procedure. • Programs are divided into what are known as objects. • Data structures are designed such that they characterize the objects. • Functions that operate on the data of an object are ties together in the data structure. • Data is hidden and cannot be accessed by external function. • Objects may communicate with each other through function. • New data and functions can be easily added whenever necessary. • Follows bottom up approach in program design.
Week 2 • Introduction to C++ Basics • Data types • Variables • Identifiers • Loops • Sample Programs • Enhancements of C++ over C
What is C++? • C++ is a compiled, object-oriented language • It is the “successor” to C, a procedural language • (the “++” is called the successor operator in C++) • C was derived from a language called B which was in turn derived from BCPL • C was developed in the 1970’s by Dennis Ritchie of AT&T Bell Labs • C++ was developed in the early 1980’s by BjarneStroustrup of AT&T Bell Labs. • Most of C is a subset of C++
Data Types • Enumeration Data Types • Declaration • Assignment • Operations • Looping with Enumeration Types • Anonymous Data Types • The typedefstatement • Namespaces • The string type
Enumeration Data Types • A data type is • A set of values together with • A set of operations on those values. • In order to define a new simple data type, called enumerationtype, we need: • A name for the data type. • A set of values for the data type. • A set of operations on the values.
Declaration of Enumerated Types • Consider the colors of the rainbow as an enumerated type:enum rainbowColors = { red, orange, yellow, green, blue, indigo, violate } • The identifiers between { } are called enumerators • The order of the declaration is significantred < orange < yellow …
Namespaces • We usually place the using directive in global scope • All blocks { . . . } then have identifiers available from the std namespace
The string type • Some functions are available string name, title;name = "Alexander";cout << name.length()<<endl;cout << name.find('x') <<endl;cout << name.substr(1,3) <<endl;title = "Big Cheese";title.swap(name);cout << name<<endl; Guess what will be the output of these lines of code
Operators • Operators • Assignment (=) • The assignment operator assigns a value to a variable. • a = 5; • Arithmetic operators ( +, -, *, /, % ) • The five arithmetical operations supported by the C++ language are: • + addition • - subtraction • * multiplication • / division • % modulo • Compound assignment (+=, -=, *=, /=, %=, >>=, <<=, &=, ^=, |=) • Increase and decrease (++, --) • Relational and equality operators ( ==, !=, >, <, >=, <= ) • Logical operators ( !, &&, || ) • Conditional operator ( ? ) • Bitwise Operators ( &, |, ^, ~, <<, >> ) • Standard Output (cout)
Identifiers • Names for various entities used in a program; used for... • Variables: values that can change frequently & which stores the data types.int a; float b;. • Constants: values that never changes intpi=3.14; • Functions: programming units that represents complex operations. • Parameters: values that change infrequently
Identifiers (Cond..) • Comments : Comments can be written using // comments or /* comments */ • The identifier number is declared as being of data type int, or integer • Include<iostream> this includes the input and output functions to the program like cout,cin • Assignment is an operation that assigns right side value to the left side. • C=a+b; All ANSI C++ programs must include this directive. This will bring all the identifiers defined in std to the current global scope. Using and namespace are the new keyword of C++.
Loops iterative While Loop
Sample C++ Program #include <iostream.h> int main() { // Declarations // Statements return 0; }
Braces { }enclose the body of the function • Declarations and statements • Main body of function (or main part) • “//” represents the start of a comment • Return statement • specifies the value the function returns • All (almost) declarations and statements end with a semi-colon “;”
Basic C++programs(Exercise) • Do the following C++ programs • Program to input int, float, char values and print same as out put. • Program for the following formula • Z=[(A+B)*C]/E
Basic Concepts of Object Oriented Programming or C++ Enhancements • Objects • Classes • Data abstraction and encapsulation • Inheritance • Polymorphism • Dynamic binding • Message passing
Week 3 Abstract Mechanisms • Classes • Objects • Data Members • Member functions • Sample Programs
Classes & Objects(Abstract Mechanism) • A class is an expanded concept of a structure: instead of holding only data, it can hold both data and functions. • An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the • Class can not be implemented till the objects are initialized . • The memory location allocated to class depends upon the number of objects are initialized. • Class is defined as the collection of similar objects . • One or more classes • Each containing data members and member functions.
Classes & Objects(Abstract Mechanism) • Class contains 3 parts class name, class attributes, class functions • Object is a part of class • If fruit has been defines as a class, then the statement • Fruit Mango; • Will create an object mango belonging to the class fruit.
Data Abstraction and Encapsulation • The wrapping up of data and function into a single unit (called class) is known as encapsulation. • This insulation of the data from direct access by the program is called data hiding or information hiding.
Week 4 Constructors & Destructors • Object creation & data initialization using constructors. • Default constructors • Destructors • Passing parameters using constructors.
Constructors • Constructors are used to initialize the values of class data members. • Constructors is one the feature of Object oriented Programming language. • Constructors are same like as functions but no return type. • Constructors are same name like a class name. • Every class has a one constructor at least if no constructors are defined then default constructor is created • Constructor are treated as special function of class. • Constructor implements the classes.
Syntax for constructors • Eg. Class Student • { • Intrno; • Char Name [20]; • Void getdata(); • Void printdata(); • } • Student()//Constructor. • { • Rno=12; • Name=”mujtaba”; • }
Destructor • The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed, either because its scope of existence has finished (for example.. • The destructor must have the same name as the class, but preceded with a tilde sign (~) and it must also return no • Value • ~student()// the destructor of student. • Constructor operation • Constructor creation, Destroying, Overloading, Copy constructor. And so on.
Default Constructor:- Default Constructor is also called as Empty Constructor which has no arguments and It is Automatically called when we creates the object of class. • Parameterized Constructor :- This is Another type Constructor which has some Arguments and same name as class name but it uses some Arguments So For this We have to create object of Class by passing some Arguments at the time of creating object with the name of class. When we pass some Arguments to the Constructor then this will automatically pass the Arguments to the Constructor and the values will retrieve by the Respective Data Members of the Class.
Copy Constructor:- This is also Another type of Constructor. In this Constructor we pass the object of class into the Another Object of Same Class. As name Suggests you Copy, means Copy the values of one Object into the another Object of Class .This is used for Copying the values of class object into an another object of class So we call them as Copy Constructor and For Copying the values We have to pass the name of object whose values we wants to Copying and When we are using or passing an Object to a Constructor then we must have to use the & Ampersand or Address Operator.
Access Specifiers • There are three types of access specifiers available in C++ they are public private and protected. • There are three access specifies are given by C++.1. Private: which disallow the accessibility of block outside the class2. Public: which allow the accessibility to outside of the class by any function3.Protected: which restrict the accessibility upto derived class onlywith the proper use of access specifier the data can be hidden from unauthorized access.
Syntax for Access specifiers • Class name_of _class • { • private : variable declaration; // data member • Function declaration; // Member Function (Method) • protected: Variable declaration; • Function declaration; • public : variable declaration; • Function declaration; • };
MEMBER FUNCTION DEFINITION • Class definition. It describes both data members and member functions. • (ii) Class method definitions. It describes how certain class member functions are coded. • We have already seen the class definition syntax as well as an example. • In C++, the member functions can be coded in two ways : • (a) Inside class definition (In line functions) • (b) Outside class definition using scope resolution operator (::)
STATIC CLASS MEMBERS • Only a single copy of the static data member is used by all the objects. • (ii) It can be used within the class but its lifetime is the whole program. • For making a data member static, we require : • (a) Declare it within the class. • (b) Define it outside the class.
Week 6 • Polymorphism & its types • Static Polymorphism. • Function Overloading & operator Overloading. • Dynamic Polymorphism • Virtual functions
Polymorphism • In programming languages, polymorphism means that some code or operations or objects behave differently in different contexts. • For example, the + (plus) operator in C++: • 4 + 5 <-- integer addition • 3.14 + 2.0 <-- floating point addition • s1 + "bar" <-- string concatenation!
Polymorphism • Polymorphism is the ability to use an operator or method in different ways. • Polymorphism gives different meanings or functions to the operators or methods. • Poly, referring to many, signifies the many uses of these operators and methods. • A single method usage or an operator functioning in many ways can be called polymorphism. • Polymorphism refers to codes, operations or objects that behave differently in different contexts.
Advantages of the concept of Polymorphism • Helps in reusability of code. • Provides easier maintenance of applications. • Helps in achieving robustness in applications • Polymorphism is divided into 2 types static binding and dynamic binding • Static binding is allocating the memory in static where as dynamic binding is giving the memory at run time. • Static binding example is function and operator overloading • Dynamic binding example is virtual functions.
Types of Polymorphism: • C++ provides three different types of polymorphism. • Virtual functions • Function name overloading • Operator overloading • In addition to the above three types of polymorphism, there exist other kinds of polymorphism: • run-time • compile-time • ad-hoc polymorphism • parametric polymorphism
Function Overloading • The process of using same function name to different functions. • It is one type of polymorphism • In function overloading same function name can be used to different function. • In spite of using same function name the function respond correctly because of there type of parameters but not the function name. • In function overloading static or late binding is used.
Virtual Functions • C++ virtual function is, • A member function of a class • Declared with virtual keyword • Usually has a different functionality in the derived class • A function call is resolved at run-time