Unit I C
Unit I C
1,0
Machine Language
Assembly Language
Procedure - Oriented
Object Oriented Programming
Main Program
Function-4 Function-5
Object A
Data Object C
Data
Function
Function
7/2/2024 Object Oriented Concepts using C++ 6
Features of OOP
• Programming language with OOP support has to fulfill these
features
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
Mobile
Mobile mbl1 ;
Mobile mbl2 ;
7/2/2024 Object Oriented Concepts using C++ 9
Class
class Mobile
{
private:
string IMEICode, SIMCard, Processor;
Mobile int InternalMemory;
Class
bool IsSingleSIM;
public:
Properties
void GetIMEICode() {
Processor cout << "IMEI Code - IEDF34343435235";
IMEICode }
IsSingleSIM void Dial() {
Methods cout << "Dial a number";
}
Dial void Receive() {
Receive
cout << "Receive a call";
SendMessage
GetWifiConnection }
ConnectBlueTooth void SendMessage(){
GetIMEICode cout << "Message Sent";
}
7/2/2024 Object Oriented Concepts using C++ 10
}
● A Class is a plan which describes the object.
○ We call it as a blue print of how the object should be represented.
● Mainly a class would consist of a name, attributes & operations.
● A Mobile can be a class which has some attributes like Profile Type, IMEI Number,
Processor, etc. & operations like Dial, Receive & SendMessage.
11
Abstraction
• Abstraction - only show relevant details and rest all hidden
– its most important pillar in OOPS as it is providing us the technique to
hide irrelevant details from User
• Dialing a number calls some method internally which concatenate the
numbers and displays it on screen but what is it doing we don’t know.
• Clicking on green button actual send signals to calling person’s mobile
but we are unaware of how it is doing.
void Dial()
{
//Write the logic
cout << "Dial a number";
7/2/2024
} Object Oriented Concepts using C++ 12
Encapsulation
• Encapsulation is defined as the process of enclosing one or more details from
outside world through access right.
– It specifies how much access should be given.
• Both Abstraction & Encapsulation works hand in hand because Abstraction
says what details to be made visible & Encapsulation provides the level of
access right to that visible details. i.e. – It implements the desired level of
abstraction.
private:
string IMEICode = "76567556757656";
Samsung
iPhone 6
S5
• We can build the programs from standard working modules that communicate with one
another, rather than having to start writing the code from scratch which leads to saving
of development time and higher productivity,
• It is very easy to partition the work in a project based on objects.
• OOP language allows to break the program into the bit-sized problems that can be
solved easily (one object at a time).
• The new technology promises greater programmer productivity, better quality of
software and lesser maintenance cost.
• OOP systems can be easily upgraded from small to large systems.
• It is possible to map the objects in problem domain to those in the program.
• The principle of data hiding helps the programmer to build secure programs which
cannot be invaded by the code in other parts of the program.
• By using inheritance, we can eliminate redundant code and extend the use of existing
classes.
• The data-centered design approach enables us to capture more details of model in an
implementable
7/2/2024 form. Object Oriented Concepts using C++ 20
Application of OOP
• Real-business system are often complex and contain many objects
with complicated attributes and methods.
• Some of the areas of application of OOPs are:
– Real-time system
– Simulation and modeling
– Object-oriented data bases
– AI and expert systems
– Neural networks and parallel programming
– Decision support and office automation systems
– CAD/CAM systems
7/2/2024 Object Oriented Concepts using C++ 21
Introduction to C++ ?
• C++ is a versatile language for handling very large programs
including editors, compilers, databases, communication
systems and any complex real-life applications
– C++ allows create hierarchy related objects to build special object-
oriented libraries which can be used later by many programmers.
– the C part of C++ gives the language the ability to get close to the
machine-level details.
– C++ programs are easily maintainable and expandable
Preprocessor
Object Code
(program.obj)
Linker
Executable file
(program.exe)
28
7/2/2024 Object Oriented Concepts using C++
The Syntactic Structure of a C++ Function
30
7/2/2024 Object Oriented Concepts using C++
Example of User-defined
C++ Function
Function header
31
7/2/2024 Object Oriented Concepts using C++
Example of User-defined
C++ Function
Function header Function body
32
7/2/2024 Object Oriented Concepts using C++
Function Signature
• The function signature is actually similar to the function header
except in two aspects:
– The parameters’ names may not be specified in the function
signature
– The function signature must end with a semicolon
• Example Unnamed Semicolon
Parameter ;
double computeTaxes(double) ;
34
7/2/2024 Object Oriented Concepts using C++
Example
#include <iostream> double computeTaxes(double income){
#include <string> if (income<5000) return 0.0;
using namespace std; return 0.07*(income-5000.0);
// Function Signature }
double getIncome(string); double getIncome(string prompt){
double computeTaxes(double); cout << prompt;
void printTaxes(double); double income;
void main() cin >> income;
{ return income;
// Get the income; }
double income = getIncome("Please enter the employee void printTaxes(double taxes){
income: "); cout << "The taxes is $" << taxes << endl;
// Compute Taxes }
double taxes = computeTaxes(income);
// Print employee taxes
printTaxes(taxes);
} 35
7/2/2024 Object Oriented Concepts using C++
Default Arguments in Function
// C++ Program to demonstrate working of default
Case 1: No argument passed Case 2: First argument passed argument
void temp (int = 10, float = 8.8); void temp (int = 10, float = 8.8); #include <iostream>
int main() { int main() { using namespace std;
temp(); temp(6); void display(char = '*', int = 1);
} } int main()
void temp(int i, float f) { void temp(int i, float f) { {
……… ……… cout << "No argument passed:\n";
} } display();
cout << "\nFirst argument passed:\n";
Case 4: Second argument passed display('#');
Case 3: All arguments passed cout << "\nBoth argument passed:\n";
void temp (int = 10, float = 8.8);
void temp (int = 10, float = 8.8); int main() { display('$', 5);
int main() { return 0;
temp(3.4);
temp(6, -2.3); }
}
} void display(char c, int n) {
void temp(int i, float f) {
void temp(int i, float f) { for(int i = 1; i <= n; ++i) {
………
……… cout << c;
}
} i = 3, f=8.8 }
7/2/2024 Object Oriented Concepts using C++ 36
Because, only the second argument cannot be passed. The cout << endl;
parameter will be passed as the first argument.
• In C++ programming, you can provide default values for
function parameters.
• The idea behind default argument is simple. If a function is
called by passing argument/s, those arguments are used by the
function.But if the argument/s are not passed while invoking a
function then, the default values are used.
• Default value/s are passed to argument/s in the function
prototype.
37
Reference Variables
• A reference is an alias, or an alternate name to an existing variable
int x = 5;
int &z = x;// z is another name for x
– Must
intbe &z
initialized
; when it is declared reference must be initialized
//Error:
keyword
class class_name
{ data member
private:
variable declaration;
body
public:
function declaration; member
…. function
} ; access
7/2/2024
specifiers
Object Oriented Concepts using C++ 45
Access Specifiers
Less Restrictive
Clas
Accessible from own class s
Private Area
public Accessible from derived class
No entry to Data
Accessible from class objects
Private
area Functions
Accessible from own class
protected
Accessible from derived class
Data
Entry allowed
to
Functions
Public area
private Accessible from own class Public Area
More Restrictive
7/2/2024 * By default all items defined in the class are private 46
Object Oriented Concepts using C++
Access Specifiers
Employee
Department Manager
public:
string name;
protected:
string designation;
private:
int Age;
CommissionEmployee
SalariedEmployee
class Person {
public: //access control
string firstName; //these data members
string lastName; //can be accessed
int dateOfBirth; //from anywhere
private:
string address; // can be accessed inside the class
long int insuranceNumber; //and by friend classes/functions
protected:
string phoneNumber; // can be accessed inside this class,
int salary; // by friend functions/classes and derived classes
};
7/2/2024 Object Oriented Concepts using C++ 48
Objects
• A class provides the blueprints for objects, so basically an
object is created from a class.
• Objects of#include
a class are declared with exactly the same sort of
<iostream>
declaration asnamespace
using variablesstd; of basic types
class Box {
public:
Class double length; // Length of a box
double breadth; // Breadth of a box Public data members
double height; // Height of a box
};
int main() {
Box Box1; // Declare Box1 of type Box Objects of Box class
Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
7/2/2024 } Object Oriented Concepts using C++ 49
Objects
int main() {
Box Box1; // Declare Box1 of type Box
• The objects of class will have their Box Box2; // Declare Box2 of type Box
double volume = 0.0; // Store the volume of a box here
own copy of data members. // box 1 specification
• The public data members of Box1.height = 5.0;
Box1.length = 6.0;
Access data members
of Box1 object
objects of a class can be accessed Box1.breadth = 7.0;
Member class
Functions
Box {
public:
double length, breadth, height;
• Can be defined inside class double getVolume(void) { // Returns box volume
return length * breadth * height; Inline function
return_type function_name (parameters) }
{ double getSurfaceArea(void); // returns surface area
// function body };
} // member function definition member function declaration
• Or outside the class double Box::getSurfaceArea(void) {
….
return_type class_name::function_name (formal} parameters) member function definition
{ int main() { outside class
// function body Box Box1;
} Box1.length = 10;
accessing member functions
• Functions defined inside class are Box1.height = 20;
Box1.breadth=30;
treated as inline functions by cout << “Volume of box: “ << Box1.getVolume() << endl;
compiler cout << “Surface Area of box: “ << Box1.getSurfaceArea() <<
endl;
7/2/2024 Object Oriented }Concepts using C++ 51
Array of Objects
#include <iostream> int main()
#include <string> {
using namespace std; Student st[5];
class Student for( int i=0; i<5; i++ )
{ {
string name; cout << "Student " << i + 1 << endl;
int marks;
cout << "Enter name" << endl;
public:
void setName() st[i].setName();
{ cout << "Enter marks" << endl;
cin>>name; st[i].setMarks();
} }
void setMarks()
{
for( int i=0; i<5; i++ )
cin >> marks;
} {
void displayInfo() cout << "Student " << i + 1 << endl;
{ st[i].displayInfo();
cout << "Name : " << name << endl; }
cout << "Marks : " << marks << endl; return 0;
} }
};
Constructors
Default
Default Parametrized Copy
Parametrized
Constructor Constructor Constructor
Constructor
height = p.height;
width = p.width;
}
};7/2/2024 Object Oriented Concepts using C++ 68
Constructor Overloading
• You can have more than one constructor in a class, as long as each has a different list
of arguments
class rectangle{
private:
float height;
float width;
public:
rectangle(){
height=width=10.0;
}
rectangle(float h, float w){
height=h;
width=w;
}
7/2/2024 }; Object Oriented Concepts using C++ 69
Destructors
• A destructor is used to destroy the objects that have been created by a constructor.
• Like constructor, the destructor is a member function whose name is the same as
the class name but is preceded by a tilde.
~T();
• It is a good practice to declare destructors in a program since it releases memory
space for further use.
box
book
eraser
De-allocation
Allocation
box
book
Initialize memory:
pointer-variable = new data-type(value);
Example:
int *p = new int(25);
float *q = new float(75.25);
new returns a pointer (or memory address) to the location where the
data is to be stored.
int * myIntPtr
myIntPtr = new int;
myIntPtr
12 3
int * myIntPtr
myIntPtr = new int;
myIntPtr
*myIntPtr = 123;
0 int * myIntPtr;
myIntPtr myIntPtr = new int[4];
1 3 2 5
2
3 myIntPtr[1] = 325;
The new operator gets memory from the free store (heap).
The memory pool for dynamic memory allocation is larger than that
set aside for static memory allocation.
Dynamic memory can be returned to the free store and allocated for
storing other data at later points in a program. (reused)