SlideShare a Scribd company logo
1 of 14
Download to read offline
•A program with class
•A program with class
•Array of objects
•Concept of reference
•Dynamic memory allocation
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
A program with class
class point
{
int x,y; //data members by default private
public:
void input(int a, int b)
{
{
x=a;
y=b;
}
void output(void)
{
cout<<“x=”<<x<<“n”; //print value of x on output screen
cout<<“y=”<<y;
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
A program with class Cont..
int main()
{
point p1, p2; // object creation
p1.input(25,30);// member function calling
p2.input(55,60);
p1.output(); //print values of x and y for object p1
p1.output(); //print values of x and y for object p1
p2.output();
return 0;
}
Output:
x=25
y=30
x=55
y=60
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Array of objects
 Array: Collection of elements of same data type.
 If elements are of class type, then it is called array of objects.
 Example-
class employee
{
{
char name[20];
int age;
public:
void getdata( );
void putdata( );
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Array of objects
 Class “employee” used to create different categories of
employees. Example-
employee faculty[3]; // array of faculty
employee staff[5]; // array of staff
employee staff[5]; // array of staff
faculty[i].getdata(); // input data for ith faculty
staff[i].getdata(); // input data for ith staff
faculty[i]. putdata(); // display data of ith faculty
staff[i].putdata(); // display data of ith staff
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Concept of reference variables
 provides an alias (alternative name) for a previously
defined variable.
 Syntax:
data_type & reference_name=variable_name;
data_type & reference_name=variable_name;
 Example-
int x = 10;
int & y = x; // y is reference variable for x;
 Both variables refer to the same memory location.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Concept of reference variables
cout<<x; // print 10
cout<<y; // print 10
x= x+5;
cout<<x; // print 15
cout<<x; // print 15
cout<<y; // print 15
y=0;
cout<<x; // print 0
cout<<y; // print 0
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Application of reference variables
 In passing arguments to functions.
 Example-
void swap(int x, int y) //swap values of two variables
{ int t=x; x=y; y=t;}
{ int t=x; x=y; y=t;}
int main()
{
int a=5, b=10;
swap(a,b); // function call by value
cout<<a<<b; // print 5 and 10
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Application of reference variables
void swap(int &x, int &y) //swap values of two variables
{ int t=x; x=y; y=t;}
int main()
{
{
int a=5, b=10;
swap(a,b); // function call by reference
cout<<a<<b; // print 10 and 5
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 Memory allocated at run-time.
 “new” operator for memory allocation
 “delete” operator to free the allocated memory
Syntax:
 Syntax:
pointer-variable = new datatype;
 Example-
int *p=new int; // allocate integer size memory
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 *p = 10; // assign value 10 to newly created
memory
 int *p=new int(10);// initialize the memory with value
10
10
 new can be used to create a memory space for any data
type including user defined such as arrays, structures,
and classes.
 int *p=new int[15];// creates memory for an array of 15
integer elements
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 For multi-dimensional arrays, all the array sizes must
be supplied.
 Example-
int *q=new int[5][3][4];// legal
int *q=new int[5][3][4];// legal
int *q=new int[m][3][4];// first dimension may be
variable, all other must be constant.
int *q=new int[5][3][ ];// illegal
int *q=new int[ ][3][4];// illegal
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 When allocated memory is no longer needed, it is
destroyed to release the memory space for reuse.
 Syntax-
delete pointer-variable;
delete pointer-variable;
 Example-
delete p; // free the memory pointed by pointer p.
delete [ ] q;// free the entire array pointed by pointer q.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Thank You
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.

More Related Content

Similar to C++ Class, Objects, Arrays and Memory Management

C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.Mohamed Fawzy
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and DestructorsKeyur Vadodariya
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Anton Kolotaev
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++sandeep54552
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxSirRafiLectures
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocationKumar
 
An introduction to thrust CUDA
An introduction to thrust CUDAAn introduction to thrust CUDA
An introduction to thrust CUDAAdrien Wattez
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingHock Leng PUAH
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console ProgramHock Leng PUAH
 

Similar to C++ Class, Objects, Arrays and Memory Management (20)

See through C
See through CSee through C
See through C
 
Learning c++
Learning c++Learning c++
Learning c++
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Oop assignment 02
Oop assignment 02Oop assignment 02
Oop assignment 02
 
C programming day#2.
C programming day#2.C programming day#2.
C programming day#2.
 
Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++Generic programming and concepts that should be in C++
Generic programming and concepts that should be in C++
 
Chapter1.pptx
Chapter1.pptxChapter1.pptx
Chapter1.pptx
 
Constructor and Destructors in C++
Constructor and Destructors in C++Constructor and Destructors in C++
Constructor and Destructors in C++
 
OOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptxOOP-Lecture-05 (Constructor_Destructor).pptx
OOP-Lecture-05 (Constructor_Destructor).pptx
 
Review functions
Review functionsReview functions
Review functions
 
Dynamics allocation
Dynamics allocationDynamics allocation
Dynamics allocation
 
Operator overloading
Operator overloadingOperator overloading
Operator overloading
 
Class ‘increment’
Class ‘increment’Class ‘increment’
Class ‘increment’
 
w10 (1).ppt
w10 (1).pptw10 (1).ppt
w10 (1).ppt
 
An introduction to thrust CUDA
An introduction to thrust CUDAAn introduction to thrust CUDA
An introduction to thrust CUDA
 
C++11 & C++14
C++11 & C++14C++11 & C++14
C++11 & C++14
 
Getting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem SolvingGetting Started - Console Program and Problem Solving
Getting Started - Console Program and Problem Solving
 
SPF Getting Started - Console Program
SPF Getting Started - Console ProgramSPF Getting Started - Console Program
SPF Getting Started - Console Program
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 

Recently uploaded

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️anilsa9823
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 

Recently uploaded (20)

Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 

C++ Class, Objects, Arrays and Memory Management

  • 1. •A program with class •A program with class •Array of objects •Concept of reference •Dynamic memory allocation Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 2. A program with class class point { int x,y; //data members by default private public: void input(int a, int b) { { x=a; y=b; } void output(void) { cout<<“x=”<<x<<“n”; //print value of x on output screen cout<<“y=”<<y; } }; Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 3. A program with class Cont.. int main() { point p1, p2; // object creation p1.input(25,30);// member function calling p2.input(55,60); p1.output(); //print values of x and y for object p1 p1.output(); //print values of x and y for object p1 p2.output(); return 0; } Output: x=25 y=30 x=55 y=60 Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 4. Array of objects  Array: Collection of elements of same data type.  If elements are of class type, then it is called array of objects.  Example- class employee { { char name[20]; int age; public: void getdata( ); void putdata( ); }; Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 5. Array of objects  Class “employee” used to create different categories of employees. Example- employee faculty[3]; // array of faculty employee staff[5]; // array of staff employee staff[5]; // array of staff faculty[i].getdata(); // input data for ith faculty staff[i].getdata(); // input data for ith staff faculty[i]. putdata(); // display data of ith faculty staff[i].putdata(); // display data of ith staff Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 6. Concept of reference variables  provides an alias (alternative name) for a previously defined variable.  Syntax: data_type & reference_name=variable_name; data_type & reference_name=variable_name;  Example- int x = 10; int & y = x; // y is reference variable for x;  Both variables refer to the same memory location. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 7. Concept of reference variables cout<<x; // print 10 cout<<y; // print 10 x= x+5; cout<<x; // print 15 cout<<x; // print 15 cout<<y; // print 15 y=0; cout<<x; // print 0 cout<<y; // print 0 Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 8. Application of reference variables  In passing arguments to functions.  Example- void swap(int x, int y) //swap values of two variables { int t=x; x=y; y=t;} { int t=x; x=y; y=t;} int main() { int a=5, b=10; swap(a,b); // function call by value cout<<a<<b; // print 5 and 10 return 0; } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 9. Application of reference variables void swap(int &x, int &y) //swap values of two variables { int t=x; x=y; y=t;} int main() { { int a=5, b=10; swap(a,b); // function call by reference cout<<a<<b; // print 10 and 5 return 0; } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 10. Dynamic memory allocation  Memory allocated at run-time.  “new” operator for memory allocation  “delete” operator to free the allocated memory Syntax:  Syntax: pointer-variable = new datatype;  Example- int *p=new int; // allocate integer size memory Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 11. Dynamic memory allocation  *p = 10; // assign value 10 to newly created memory  int *p=new int(10);// initialize the memory with value 10 10  new can be used to create a memory space for any data type including user defined such as arrays, structures, and classes.  int *p=new int[15];// creates memory for an array of 15 integer elements Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 12. Dynamic memory allocation  For multi-dimensional arrays, all the array sizes must be supplied.  Example- int *q=new int[5][3][4];// legal int *q=new int[5][3][4];// legal int *q=new int[m][3][4];// first dimension may be variable, all other must be constant. int *q=new int[5][3][ ];// illegal int *q=new int[ ][3][4];// illegal Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 13. Dynamic memory allocation  When allocated memory is no longer needed, it is destroyed to release the memory space for reuse.  Syntax- delete pointer-variable; delete pointer-variable;  Example- delete p; // free the memory pointed by pointer p. delete [ ] q;// free the entire array pointed by pointer q. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 14. Thank You Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.