Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Object Oriented Programming C++ Notes

The University of Faisalabad - Artificial Intelligence - 2023-BS-AI-017 - Lecture Notes by Sir Javed

Uploaded by

potatolazy00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Object Oriented Programming C++ Notes

The University of Faisalabad - Artificial Intelligence - 2023-BS-AI-017 - Lecture Notes by Sir Javed

Uploaded by

potatolazy00
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Name: Humna Imran

Roll Number 017


Section: A
Department: Artificial Intelligence

Logic building topics Courses Python


If else PF Java
loops OOPS
DS
OS ----> C++

OOPS ---> Functions + Structures


• Pointers
• Classes
• Objects
• Function Overloading
• Operator Overloading
• Constructor
• Destructor
• Inheritance & Types
• Virtual Functions -----> Abstraction
• Polymorphism
Imp for interview
• Array and structure and class
• Array multiple data - same data type
• Structure collection of data type - multiple data type store with one name -
user define data type
• Class and structure have different data type but concepts of oops
(abstraction , polymorphism, encapsulation, inheritance) do not applied on
structures only applied on class

Structures
• Can come before main function and after main function
• User defined data type
• Collection of multiple variables of different data types

Declaration of structure

Define structure Variables

Memory Blocks

Accessing member of structure

};
int main (){
For Example: vehical car; //define structure variable
//Acessing number of structure
#include <iostream>
cout << "Enter Car Brand:" << endl;
using namespace std;
cin >> car.brand;
struct vehicle //declaration structure
cout << "Enter Car Model:" << endl;
{
cin >> car.model;
string brand;
cout << "Enter Years of issue: " << endl;
string model;
cin>> car.years;
int years;
}
Union (Concept)

• Like structure but we make variable in structure and memory allocated in multiple cells
• Difference variable allocated on basis of biggest/highest data member
• Memory management
• can't apply oops concept
• Can't apply construct destruct concepts

Here is the code of Struct:

And By Changing "Struct" To Union, Here is the Code of Union:

Union (Lecture By Sir)

Union is a user defined data type


All members have same same memory location
The size of union is equal to largest member (data-type)
User can't do manipulation in union
Syntax

Example

Enum (Concept)

• Like Struct, class, union (user defined)


• Enum is also user defined but tell number of states countable integers
• Maximum using in abstraction/ or increasing abstraction
• Check the verification of task if it is correct it move to next step
• Like if we enter password if it is correct it login to account otherwise show
error
• Abstraction : show basic things to stake holders

Enum (Enumeration)
~ Lecture By sir

• User defined Data Type


• Use to select One Value from all possible sets of value (e.g. button on/off
of login page)
• Use to increase the level of abstraction
• Consist of integral constant (measurable vale) - for example:
Temperature, iron, ice cream flavors in machine

Syntax

Example

Output:
2

(it is telling index, but in array we don't use abstraction)

By changing index
Oops Concept
Inheritance
Basically Approach not a language
Two types of programing • Parent Child Same Characteristics
• Procedural
• Object oriented Polymorphism
Programming based on object • State Object in different aspects
Pillar of oops are four
• Inheritance Encapsulation
• Polymorphism • Data Building
• Abstraction
• Encapsulation Abstraction
• Data Hiding
Class:
Class and object basic components Object
• Class collection of objects
• Object is a real world Entity Like person,
• Representing objects
Things etc.
• Memory will allocated as we create object But Do not created
• Objects have specific properties(states)
when class is created
and functions(behaviors)
Methods and Data members and functions are studied in function
• When object is created memory is allocated
Private public Protected

Syntax For Executing Object:


Objects:
Objects are Real Thing Entity
States
Behaviors
E.g. fan behaviors: Speed slow, On, Off

Executing The member Function:


Lecture By Sir
Object Oriented Programing
• Is an Engineering Approach use to develop the program on the
basis of Object
• Is an programming technique associated with class, object and
revolving round other aspects such as inheritance, Class:
polymorphism, abstraction and encapsulation User defined datatypes
Language: C++, C#, Java, Python, etc. Collection of objects with specific states and
Features of Oops behaviors
Objects of class are called it's instances
• Easy to Understand and Modify
The function in class are called it's methods
• Reuseable
Data Type/ variables are called data members
• Objects
• Classes
Syntax
• Inheritance
• Polymorphism
• Encapsulation
• Abstraction
Function Function without class
• We can define function in class Scope resolution operator "::" is used
 Within class
 Without class Syntax

• Declaration will be within class

Lecture by Sir
Function in class
Function can must be declare with in class but can be
defined within or without the class

Function within class


Syntax

Example

Example
Constructor Example

(Concept)
• Name will be same as class
• Return type is not used/Defined
• Main point: As object is created the body of constructor is
executed automatically no need to call function

Types:
• Default
• Parametrized
• Copy

Lecture by Sir:
• Special Member Function
• Whose name is same as class name
• Constructor call automatically when object is created
• Constructor Do not have return type
• Can't be declared as virtual Function

Syntax

Default Constructor
The Constructor which have no arguments or no parameters
JAVA Can overload main function
C++ can't overload main function and return type Constructor Overloading
Default data type of function can be "void" or "int" The process of Declaring multiple
constructor in a program with same but
Recursion different in
Recursion is a programming technique in which the • No of parameters
function calls itself • Type of Parameters
• Sequence of parameters
Recursive function
The function that calls itself Example:
Used instead of loop, used in stack(last In last out)
Less Efficient
Requires more memory

Example

Paramerterized constructor
Jis me parameter pass kerjai
Copy constructor Syntax:
Through copy constructor we initialize the variable of Static data_type variable_name;
object with value of variable of other object
Conditions: Example:
Same as class name Static int a;
Data-type should be same
Const
Object pass as argument in function
Syntax ▪ Fix value
▪ Will not upgrade
▪ Scope will different
▪ Can't be modified

Syntax:
Const data_type variable_name;
Static Data Members
Two types of members Example:
• Static Const int a;
▪ Assign value when we
initialize Static Member function
▪ Scope object will same
 Declare with keywords static
▪ Concept of const will not
 Can be access with/without creating of objects of class
applied
 Virtual function can't use as static
▪ Will use in management
 Static member functions are mostly used to store
▪ We can access then
information that are shared to all the objects.
without creating object
 They can't be accessed by pointer based objects
▪ Memory management
▪ Static keyword
• Normal
▪ Assign value
▪ Scope of object is
different
• Const
▪ Fix value
▪ Will not upgrade
▪ Scope will different
▪ Can't be modified
Lecture by Sir
Static Data members
• Use static keywords
• Scope of static data member
remain same for all objects of
class
• Static data members can use for
memory management
• Static Data Member can access
without creating objects
• Can Initialize before main().
Friend Function (Concept)
 Friend function is a function that access private /
protected class members
 Private and protected are used in data hiding
 We can't use it as a normal function and use special
function for it
 We do not need use to dot operator to access it
 Class object is passed as an argument
 Disadvantage: if using multiple friend function data
can be breach

Friend function (Lecture By Sir)


• Special Member function used to access private/
protected member of class
• Not - Member Function
• Can be declared inside the class by using friend
keyword
• But Defined Outside the class
• Friend keyword is used in declaration not in definition
• Friend function called like ordinary function doesn't
use dot operator
• Use object as argument

• Advantages
 Accessible member of class without
inheritance
 Act like bridge between classes

• Disadvantages
 Violates the law of data-hiding
Finals Example:

Inheritance (Concept)
Use properties of one class to other

• First class : parent class


• Second class : derived class , super class
sub class

Relationship of inheritance
• Single level: one child class one parent
• Multi-level: one parent multiple child
• Multiple level: two parents and one child
class
• Hybrid: multiple parent class multiple
children class

Lecture by sir:
The process of accessing the data member, member
function of one class in another class
Parent class (Base Class)
Child Class(Derived Class)

Levels of Inheritance
Single level inheritance (One to One)
Multi-level inheritance (One to Many)
Multiple level inheritance (Many to One)
Hybrid level inheritance (Many to Many)

Single Level Inheritance


One-to-one
Syntax:
Mode of inheritance
Public:
Member are accessible from outside the class and member can be accessed from anywhere

Private:
Member can't be accessed from outside the class i.e. members are private to that class only

Protected:
Member can't be accessed from outside the class but they can be accessed in inherited class or derived classes

Syntax:
Class A
{
Public:
//member functions

Protected:
//member function

Private:
//member function

};
Class B: public A
Class B private A
Class B: protected A

Accessibility in public inheritance


Accessibility Private members Protected member Public members

Base class Yes Yes Yes


Derived Class No Yes Yes

Protected Inheritance
Accessibility in protected inheritance
Accessibility Private member protected member Public member

Base class Yes Yes Yes


Derive class No Yes Yes(inherited as protected variables

Private Inheritance
Accessibility in private inheritance
Accessibility Private member protected member Public member

Base class Yes Yes Yes


Derive class No Yes(inherited as private variables) Yes(inherited as private variables

Protected can access through inheritance


Private can access by friend function
Pointers (concept) Function Overloading
Function with same name but different in
As we declare a variable it has its values
• Number of parameters
pointers save address and value both of other variable
• Types of parameters
If you want to find the value of a variable "&" is used,
• Sequence of parameter
the address is in hexadecimal

Lecture by sir:
Pointer:
Is variable that store the memory address
Is a variable that store the address of other variable

Example:

#include <iostream>
using namespace std;

int main()
{
int x = 30;
int *ptr;
ptr = &x;

cout << x << endl


<< &x << endl
<< ptr << endl
<< *ptr;
}

Polymorphism (concept)
Polymorphism means many forms
Aik he per son teacher b hota hay son b hota hay to ye
aik he person ki many forms hoti hain
Same like object has many forms according to
conditions
Object behaves differently according to condition

It has two types


Static /compiled time
Dynamic / run time
Late Binding/Early Binding

Lecture by sir
Polymorphism
Many forms
The ability of function/object behave different
according to condition
Operator Overloading void show()
{
Built in data type operators for data types (string, int) are [*,+,==] etc.
cout << "Volume of n = " << n;
When we apply operators in class they are different
}
Operator based Function need to be declared like two objects increment
We need to make a function to do that
void operator++()
Operator based function must not be static
{
Unary operator
n = n + 1;
• That have one Operand
}
Binary, Comparison Operators
};

Lecture by Sir
int main()
The way to changing operator to for user define data type (class/struct) {
Number obj;
Rules for operator Overloading obj.show();
• For Normal Function binary operator have one argument and for ++obj;
Unary Operator No Argument obj.show();
• For Friend Function Binary operator have two arguments and unary }
operator have one argument
Operator Overloading with return type
• Operator function must be global/friend function
• These operators can’t be overload: #include <iostream>
 . (dot Operator) using namespace std;
 , (Comma Operator) class Number
 :: (Resolution Operator) {
 ? (Question Operator) int n;
 :
Unary Operator Overloading public:
That Have one operand Number()
{
Syntax: n = 5;
}
void show()
return_type operator symbol()
{
{
cout << "Volume of n = " << n;
//statement;
}
}
Number operator--()
Code: {
Number temp;
#include <iostream> n--;
using namespace std; temp.n = n;
class Number return temp;
{ }
int n; };
int main()
public: {
Number() Number obj1, obj2;
{ obj1.show();
n = 5; obj2.show();
}
obj2 =--obj1;
obj1.show();
Without return type obj2.show();
With return type }
Pointer to Object (concept)
int main()
Object is like a variable
{
We can Also access objects from pointer test obj;
test *pobj;
Lecture by sir pobj = &obj;

Code: (*pobj).in();
(*pobj).out();
Method # 1: }
#include <iostream>
using namespace std; Method # 3:
#include <iostream>
class test using namespace std;
{
int n; class test
{
public: int n;
void in()
{ public:
cout << "Enter Value of n: "; void in()
cin >> n; {
} cout << "Enter Value of n: ";
cin >> n;
void out() }
{
cout << "The value of n: " << n; void out()
} {
}; cout << "The value of n: " << n;
int main() }
{ };
test obj; int main()
test *pobj; {
pobj = &obj; test *pobj;
pobj = new test;
pobj->in(); pobj->in();
pobj->out(); pobj->out();
} }

Method # 2: Uses of Pointer Objects:


#include <iostream>
using namespace std; • Overriding
• Accessing child through parent
class test • Compile time polymorphism
{
int n;

public:
void in()
{
cout << "Enter Value of n: ";
cin >> n;
}

void out()
{
cout << "The value of n: " << n;
}
};
Overriding Virtual Function
Same name Same type of parameter then • Existing in effect but not in reality
program execute • Use to access the runtime polymorphism
Use in inheritance • Define in base class but override in child class
Function definition of overriding changed • Enable The user to access the different function with same
in Derived class function call
But overloading can be use everywhere • Do not use virtual function as static
• Don't use as constructor but use as destructor
Lecture by Sir
We declare the function in Base and Example:
derived Class with same name and #include <iostream>
parameter (Signature/Argument) using namespace std;

Syntax on vs code: class animal


{
public:
virtual void print()
{
cout << "else";
}
};
class cow : public animal
{
public:
void print ()
{
cout << "else";
}
};

int main()
{
animal *ptr;
cow c;
ptr = &c;
ptr -> print();
}
Pure Virtual Function
• Through pure virtual function we don't access class directly
• Pure virtual function can declare but not define
• Have no body
• Use to create a generic object
Example (on vs code)

• Early binding
• Late binding
Early binding happens in compile time, while late binding during runtime.

• Concreate class
• Abstract class
Abstraction Inheritance:
• Is-a->relationship
Is defined as delivering only essential information
Association(Many to Many):
Only provide user that he knows
Virtual function is used in abstraction • Has-a->relationship
• Relationship between two classes that
Lecture by sir: established through their objects

It is defined as delivering only essential


Example:
information to worldwide but hide the other
information • Books-Pages
• Teachers-Students
• Abstract class: • Doctors-Patients
• Drivers-Cars
The class which has only one pure virtual
Aggregation(One to One OR One to
• Concreate class:
Many):
Normal class that has no pure virtual
function • Has-a->relationship
• Special type of association where the objects
can exit independently
Inheritance • Loosely coupled relationship
Three terms: • No Life dependencies
• Association
• Composition Example:
• Aggregation • Student- Address
In UML Diagram we make relation between two • Room-Chairs
classes and object of class : Keyword is used : Has- • University-Departments
a-relationship
Composition (Death Relationship):
• Has-a->relationship
Association
• Strong Form of association where the life time
of one object depends lifetime of other object
• Life Dependency
Aggregation • Tightly Coupled relationship

Example:
Composition • Building- Room
• Engine-Car

• Association - Has a Relationship , union of


aggregation and composition
• Aggregation - If dependency is weak (e.g. name
of student class and address class)
• Composition - Tightly bound Relation (e.g. house
Room + Relation or Car with engine)

You might also like