SlideShare ist ein Scribd-Unternehmen logo
1 von 36
CONCEPT OF
CLASSES AND
OBJECTS
By: Amanpreet Kaur
Class
 A class is an organization of data and
functions which operate on them.
 Data structures are called data members and
the functions are called member functions.
 The combination of data members and
member functions constitute a data object or
simply an object.
Object
 Instantiation of a class.
 In terms of variables, class would be the type
and an object would be a variable.
General Structure of a class
 Class name or name of class
 Data Members
 Member functions
 Access Specifiers
 Declaring objects
Classes in C++
 A class definition begins with the keyword
class.
 The body of the class is contained within a set
of braces, { } ; (notice the semi-colon).
class class_name
{
….
….
….
};
Class body (data member
+ methods)
Any valid
identifier
class classname
{
private:
variable declarations;
function declarations;
public:
variable declarations;
function declarations;
protected:
variable declarations;
function declarations;
} obj1, obj2,…..objN;
Class name
 Name given to a particular class.
 Serves as a name specifier for the class using
which we can create objects.
 The class is specified by keyword “class”
Data Members
 Data type properties that describe the
characteristics of a class.
 We can declare any number of data members
of any type in a class.
 We can say that variables in C and data
members in C++.
 E.g. int rn;
Member functions
 Various operations that can be performed to
data members of that class.
 We can declare any number of member
functions of any type in a class.
 E.g. void read();
Outside class definition
Return type classname :: funcname(arg
declaration)
{
Function body
}
Void item ::getdata(int a,float b0
{
N=a;
C=b;
}
Inside class
Class item
{
int number;
Float cost;
Public:
Void getdat(int a,float b);
Void putdata(void);
{
cout<<number;
cout<<cost;
}
};
Making an outside function
inline
 Objective of oop is to separate details of implementation from
class definition
class item
{
public:
void getdata(int a,float b);
};
inline void item :: getdata(int a,float b)
{
n=a;
c=b;
}
Access Specifiers
 Used to specify access rights for the data
members and member functions of the class.
 Depending upon the access level of a class
member, access to it is allowed or denied.
 Within the body, the keywords private: and public:
specify the access level of the members of the
class.
 the default is private.
 Usually, the data members of a class are declared
in the private: section of the class and the
member functions are in public: section.
Classes in C++
class class_name
{
private:
…
…
…
public:
…
…
…
};
Public members or methods
private members or
methods
Private:
only members of that class have accessibility
 can be accessed only through member
functions of that class.
 Private members and methods are for
internal use only.
Public:
 Accessible from outside the class
 can be accessed through member function of
any class in the same program.
Protected:
 Stage between private and public access.
 They cannot be accessed from outside the
class, but can be accessed from the derived
class.(inheritance)
Class Example
 This class example shows how we can
encapsulate (gather) a circle information into
one package (unit or class)
class Circle
{
private:
double radius;
public:
void setRadius(double r);
double getDiameter();
double getArea();
double getCircumference();
};
No need for others classes to
access and retrieve its value
directly. The
class methods are responsible for
that only.
They are accessible from outside
the class, and they can access the
member (radius)
C++ supports three access specifiers:
public
private
protected
The public access specifier allows a class to subject its
member variables and member functions to other functions and
objects
The private access specifier allows a class to hide its
member variables and member functions from other class
objects and functions
The protected access specifier allows a class to hide
its member variables and member functions from other class
objects and functions just like private access specifier - is
used while implementing inheritance
Declaring objects:
 A class declaration only uses to build the
structure of an object.
 Declaration of an object is same as declaration
of class.
 Defining objects of class data type is known as
class instantiation(instances of class)
 When we create objects during that moment ,
memory is allocated to them.
• Ex- Circle c;
class Customer
{
void accept()
{
cout << “Accepting Customer Details” << endl;
}
void display()
{
cout << “Displaying Customer Details” << endl;
}
};
Void main()
{
Customer C1;
C1.accept();
C1.display();
getch();
}
Private member function
a private member function can only be called by
another function that is a member of its class. Even an
object cannot invoke a private function using dot
operator
class sam
{ int m; void read(void);
public:
void update(void); void write(void);
};
if s1 is an object of sam then
s1.read(); won’t work
Can be done like
void sam:: update(void)
{ read();
Arrays within class
 const int size=10;
class array
{
int a[size];
public:
void setval(void);
void display(void);
};
Memory allocation for objects
 We have studied that memory space for
objects is allocated when they are declared
and not when class is specified. All objects
belong to a class use same member functions
no separate space is allocated for member
function when object is created.
 Only space for member variables is allocated
separately for each object.
 Memory created when functions defined:
common
 Memory created when objects defined. Not
common
Static data members
 It is initialized to zero when first object of its
class is created . No other initialization is
permitted.
 Only one copy of that member is created for
entire class and shared by all objects of that
class.
 Visible only within class but lifetime in entire
program.
 Static int age;
Static class member
class item
{ static int count;
int number;
public:
void getdata(int a)
{ number = a;
count++;
}
void getcount(void)
{
cout<<count;
}
};
Int item :: count; // definition of static data member
main()
{
item a,b,c;
a.getcount();
b.getcount();
c.getcount();
a.getdata(100);
b.getdata(200);
c.getdata(300);
Cout<<“ after reading”;
a.getcount();
b.getcount();
c.getcount();
}
OUTPUT
Count:0 0 0
After reading 3 3 3
Static member function
A static function can have access to only other
static members declared in same class
Can be called using class name instead of
objects
Class-name :: function-name;
Class test
{
Int code;
Static int count;
Public:
Void setcode(void)
{
Code= ++count;
}
void showcode(void)
{ cout<<code;
}
Static void showcount(void)
{
cout<<count;
} };
int test :: count;
int main()
{ test t1,t2;
t1.setcode();
t2.setcode();
test :: showcount();
test t3;
t3.setcode();
test:: showcount();
t1.showcode();
t2.showcode();
t3.showcode();
return 0;
}
Array of objects
Void emp:: getdata();
{
Cin>>name>>age;
}
Void emp: putdata();
{
Cout<<name<<age;
}
const int s=3
Main()
{ emp m[s];
For(int i=0;i<s;i++)
{
m[i].getdata();
}
For(int i=0;i<s;i++)
{
m[i].putdata();
}
Object as function argument
 Entire object is passed as function argument.
 Pass by value: no changes made to the object.
 Only address is passed to function.
 Pass by reference: address is passed.
Class containing pointer
#include<iostream.h>
#include<conio.h>
class complex
{ float real,imag;
public:
void get();
void sum(complex*,complex*);
void show();
};
void complex::get()
{ cin>>real;
cin>>imag;
}
void complex::sum(complex
*c1,complex *c2)
{real=c1->real+c2->real; //(
member access through pointer)
imag=c1->imag+c2->imag;
}
void complex ::show()
{
cout<<real<<"+i"<<imag;
}
main()
{
complex c1,c2,c3;
c1.get();
c2.get();
c1.show();
c2.show();
c3.sum(&c1,&c2);
c3.show();
getch();
}
What is this pointer?
 Every object has a special pointer "this" which
points to the object itself.
 This pointer is accessible to all members of
the class but not to any static members of the
class.
 Can be used to find the address of the object
in which the function is a member.
 Presence of this pointer is not included in the
sizeof calculations.
class MyClass
{
int data;
public:
MyClass()
{data=100;};
void Print1();
void Print2();
};
// Not using this pointer
void MyClass::Print1() {
cout << data << endl;
}
// Using this pointer
void MyClass::Print2() {
cout << "My address = " << this << endl;
cout << this->data << endl;
}
int main()
{
MyClass a;
a.Print1();
a.Print2();
// Size of doesn't include this pointer
cout << sizeof(a) << endl;
}
OUTPUT:
100
My address = 0012FF88
100
4

Weitere ähnliche Inhalte

Was ist angesagt?

Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++rprajat007
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 augshashank12march
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan collegeahmed hmed
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++NainaKhan28
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introductionSohanur63
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++Jeff TUYISHIME
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentationnafisa rahman
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsHarsh Patel
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Majid Saeed
 
Classes and objects
Classes and objectsClasses and objects
Classes and objectsNilesh Dalvi
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#Adeel Rasheed
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functionsMarlom46
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteTushar B Kute
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member FunctionsMuhammad Hammad Waseem
 

Was ist angesagt? (20)

Class and object in C++
Class and object in C++Class and object in C++
Class and object in C++
 
Classes and objects till 16 aug
Classes and objects till 16 augClasses and objects till 16 aug
Classes and objects till 16 aug
 
Classes cpp intro thomson bayan college
Classes cpp  intro thomson bayan collegeClasses cpp  intro thomson bayan college
Classes cpp intro thomson bayan college
 
Class and object in c++
Class and object in c++Class and object in c++
Class and object in c++
 
Java class,object,method introduction
Java class,object,method introductionJava class,object,method introduction
Java class,object,method introduction
 
2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++2 lesson 2 object oriented programming in c++
2 lesson 2 object oriented programming in c++
 
Class and objects
Class and objectsClass and objects
Class and objects
 
Classes and objects in java
Classes and objects in javaClasses and objects in java
Classes and objects in java
 
Object and class presentation
Object and class presentationObject and class presentation
Object and class presentation
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)Classes in c++ (OOP Presentation)
Classes in c++ (OOP Presentation)
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes and Objects in C#
Classes and Objects in C#Classes and Objects in C#
Classes and Objects in C#
 
Data members and member functions
Data members and member functionsData members and member functions
Data members and member functions
 
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCECLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
CLASSES AND OBJECTS IN C++ +2 COMPUTER SCIENCE
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B KuteChapter 02: Classes Objects and Methods Java by Tushar B Kute
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
 
[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions[OOP - Lec 19] Static Member Functions
[OOP - Lec 19] Static Member Functions
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 

Andere mochten auch

C++ Advanced Features (TCF 2014)
C++ Advanced Features (TCF 2014)C++ Advanced Features (TCF 2014)
C++ Advanced Features (TCF 2014)Michael Redlich
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++Muhammad Waqas
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III TermAndrew Raj
 
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part IIIntro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part IIBlue Elephant Consulting
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programmingnirajmandaliya
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloadingPrincess Sam
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class DiagramHadziq Fabroyir
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++Reddhi Basu
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language CourseVivek chan
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Himanshu Kaushik
 
Five components of communication
Five components of communicationFive components of communication
Five components of communicationjumar dimas
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage classkapil078
 

Andere mochten auch (20)

C++ Advanced Features (TCF 2014)
C++ Advanced Features (TCF 2014)C++ Advanced Features (TCF 2014)
C++ Advanced Features (TCF 2014)
 
Object Oriented Programming Using C++
Object Oriented Programming Using C++Object Oriented Programming Using C++
Object Oriented Programming Using C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part IIIntro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
Intro To C++ - Class 06 - Introduction To Classes, Objects, & Strings, Part II
 
098ca session7 c++
098ca session7 c++098ca session7 c++
098ca session7 c++
 
Intro To C++ - Class 14 - Midterm Review
Intro To C++ - Class 14 - Midterm ReviewIntro To C++ - Class 14 - Midterm Review
Intro To C++ - Class 14 - Midterm Review
 
Lecture 8 Library classes
Lecture 8 Library classesLecture 8 Library classes
Lecture 8 Library classes
 
C++ Object oriented concepts & programming
C++ Object oriented concepts & programmingC++ Object oriented concepts & programming
C++ Object oriented concepts & programming
 
Fp201 unit2 1
Fp201 unit2 1Fp201 unit2 1
Fp201 unit2 1
 
Lec 26.27-operator overloading
Lec 26.27-operator overloadingLec 26.27-operator overloading
Lec 26.27-operator overloading
 
#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram#OOP_D_ITS - 8th - Class Diagram
#OOP_D_ITS - 8th - Class Diagram
 
OOP
OOPOOP
OOP
 
C pointer basics
C pointer basicsC pointer basics
C pointer basics
 
Storage Class Specifiers in C++
Storage Class Specifiers in C++Storage Class Specifiers in C++
Storage Class Specifiers in C++
 
Set Theory In C++
Set Theory In C++Set Theory In C++
Set Theory In C++
 
Complete C++ programming Language Course
Complete C++ programming Language CourseComplete C++ programming Language Course
Complete C++ programming Language Course
 
Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++Lecture 2 C++ | Variable Scope, Operators in c++
Lecture 2 C++ | Variable Scope, Operators in c++
 
Five components of communication
Five components of communicationFive components of communication
Five components of communication
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
 

Ähnlich wie class c++

Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Enam Khan
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfstudy material
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methodsfarhan amjad
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-exampleDeepak Singh
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptmanomkpsg
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ Dev Chauhan
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++Mohamad Al_hsan
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)Durga Devi
 

Ähnlich wie class c++ (20)

OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
ccc
cccccc
ccc
 
Class and object
Class and objectClass and object
Class and object
 
C++ Notes
C++ NotesC++ Notes
C++ Notes
 
Lecture 2 (1)
Lecture 2 (1)Lecture 2 (1)
Lecture 2 (1)
 
Class object
Class objectClass object
Class object
 
Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.Presentation on class and object in Object Oriented programming.
Presentation on class and object in Object Oriented programming.
 
chapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdfchapter-7-classes-and-objects.pdf
chapter-7-classes-and-objects.pdf
 
Classes and objects
Classes and objectsClasses and objects
Classes and objects
 
Classes, objects and methods
Classes, objects and methodsClasses, objects and methods
Classes, objects and methods
 
Class and object
Class and objectClass and object
Class and object
 
Chapter22 static-class-member-example
Chapter22 static-class-member-exampleChapter22 static-class-member-example
Chapter22 static-class-member-example
 
Classes
ClassesClasses
Classes
 
C++ classes
C++ classesC++ classes
C++ classes
 
classandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.pptclassandobjectunit2-150824133722-lva1-app6891.ppt
classandobjectunit2-150824133722-lva1-app6891.ppt
 
OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++ OBJECT ORIENTED PROGRAMING IN C++
OBJECT ORIENTED PROGRAMING IN C++
 
object oriented programming language by c++
object oriented programming language by c++object oriented programming language by c++
object oriented programming language by c++
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
Lecture 4. mte 407
Lecture 4. mte 407Lecture 4. mte 407
Lecture 4. mte 407
 
Unit vi(dsc++)
Unit vi(dsc++)Unit vi(dsc++)
Unit vi(dsc++)
 

Kürzlich hochgeladen

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 

Kürzlich hochgeladen (20)

Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 

class c++

  • 2. Class  A class is an organization of data and functions which operate on them.  Data structures are called data members and the functions are called member functions.  The combination of data members and member functions constitute a data object or simply an object.
  • 3. Object  Instantiation of a class.  In terms of variables, class would be the type and an object would be a variable.
  • 4. General Structure of a class  Class name or name of class  Data Members  Member functions  Access Specifiers  Declaring objects
  • 5. Classes in C++  A class definition begins with the keyword class.  The body of the class is contained within a set of braces, { } ; (notice the semi-colon). class class_name { …. …. …. }; Class body (data member + methods) Any valid identifier
  • 6. class classname { private: variable declarations; function declarations; public: variable declarations; function declarations; protected: variable declarations; function declarations; } obj1, obj2,…..objN;
  • 7. Class name  Name given to a particular class.  Serves as a name specifier for the class using which we can create objects.  The class is specified by keyword “class”
  • 8. Data Members  Data type properties that describe the characteristics of a class.  We can declare any number of data members of any type in a class.  We can say that variables in C and data members in C++.  E.g. int rn;
  • 9. Member functions  Various operations that can be performed to data members of that class.  We can declare any number of member functions of any type in a class.  E.g. void read();
  • 10. Outside class definition Return type classname :: funcname(arg declaration) { Function body } Void item ::getdata(int a,float b0 { N=a; C=b; }
  • 11. Inside class Class item { int number; Float cost; Public: Void getdat(int a,float b); Void putdata(void); { cout<<number; cout<<cost; } };
  • 12. Making an outside function inline  Objective of oop is to separate details of implementation from class definition class item { public: void getdata(int a,float b); }; inline void item :: getdata(int a,float b) { n=a; c=b; }
  • 13. Access Specifiers  Used to specify access rights for the data members and member functions of the class.  Depending upon the access level of a class member, access to it is allowed or denied.  Within the body, the keywords private: and public: specify the access level of the members of the class.  the default is private.  Usually, the data members of a class are declared in the private: section of the class and the member functions are in public: section.
  • 14. Classes in C++ class class_name { private: … … … public: … … … }; Public members or methods private members or methods
  • 15. Private: only members of that class have accessibility  can be accessed only through member functions of that class.  Private members and methods are for internal use only.
  • 16. Public:  Accessible from outside the class  can be accessed through member function of any class in the same program.
  • 17. Protected:  Stage between private and public access.  They cannot be accessed from outside the class, but can be accessed from the derived class.(inheritance)
  • 18. Class Example  This class example shows how we can encapsulate (gather) a circle information into one package (unit or class) class Circle { private: double radius; public: void setRadius(double r); double getDiameter(); double getArea(); double getCircumference(); }; No need for others classes to access and retrieve its value directly. The class methods are responsible for that only. They are accessible from outside the class, and they can access the member (radius)
  • 19. C++ supports three access specifiers: public private protected The public access specifier allows a class to subject its member variables and member functions to other functions and objects The private access specifier allows a class to hide its member variables and member functions from other class objects and functions The protected access specifier allows a class to hide its member variables and member functions from other class objects and functions just like private access specifier - is used while implementing inheritance
  • 20. Declaring objects:  A class declaration only uses to build the structure of an object.  Declaration of an object is same as declaration of class.  Defining objects of class data type is known as class instantiation(instances of class)  When we create objects during that moment , memory is allocated to them. • Ex- Circle c;
  • 21. class Customer { void accept() { cout << “Accepting Customer Details” << endl; } void display() { cout << “Displaying Customer Details” << endl; } }; Void main() { Customer C1; C1.accept(); C1.display(); getch(); }
  • 22. Private member function a private member function can only be called by another function that is a member of its class. Even an object cannot invoke a private function using dot operator class sam { int m; void read(void); public: void update(void); void write(void); }; if s1 is an object of sam then s1.read(); won’t work Can be done like void sam:: update(void) { read();
  • 23. Arrays within class  const int size=10; class array { int a[size]; public: void setval(void); void display(void); };
  • 24. Memory allocation for objects  We have studied that memory space for objects is allocated when they are declared and not when class is specified. All objects belong to a class use same member functions no separate space is allocated for member function when object is created.  Only space for member variables is allocated separately for each object.  Memory created when functions defined: common  Memory created when objects defined. Not common
  • 25.
  • 26. Static data members  It is initialized to zero when first object of its class is created . No other initialization is permitted.  Only one copy of that member is created for entire class and shared by all objects of that class.  Visible only within class but lifetime in entire program.  Static int age;
  • 27. Static class member class item { static int count; int number; public: void getdata(int a) { number = a; count++; } void getcount(void) { cout<<count; } }; Int item :: count; // definition of static data member main() { item a,b,c; a.getcount(); b.getcount(); c.getcount(); a.getdata(100); b.getdata(200); c.getdata(300); Cout<<“ after reading”; a.getcount(); b.getcount(); c.getcount(); } OUTPUT Count:0 0 0 After reading 3 3 3
  • 28. Static member function A static function can have access to only other static members declared in same class Can be called using class name instead of objects Class-name :: function-name;
  • 29. Class test { Int code; Static int count; Public: Void setcode(void) { Code= ++count; } void showcode(void) { cout<<code; } Static void showcount(void) { cout<<count; } }; int test :: count; int main() { test t1,t2; t1.setcode(); t2.setcode(); test :: showcount(); test t3; t3.setcode(); test:: showcount(); t1.showcode(); t2.showcode(); t3.showcode(); return 0; }
  • 30. Array of objects Void emp:: getdata(); { Cin>>name>>age; } Void emp: putdata(); { Cout<<name<<age; } const int s=3 Main() { emp m[s]; For(int i=0;i<s;i++) { m[i].getdata(); } For(int i=0;i<s;i++) { m[i].putdata(); }
  • 31. Object as function argument  Entire object is passed as function argument.  Pass by value: no changes made to the object.  Only address is passed to function.  Pass by reference: address is passed.
  • 32. Class containing pointer #include<iostream.h> #include<conio.h> class complex { float real,imag; public: void get(); void sum(complex*,complex*); void show(); }; void complex::get() { cin>>real; cin>>imag; } void complex::sum(complex *c1,complex *c2) {real=c1->real+c2->real; //( member access through pointer) imag=c1->imag+c2->imag; } void complex ::show() { cout<<real<<"+i"<<imag; } main() { complex c1,c2,c3; c1.get(); c2.get(); c1.show(); c2.show(); c3.sum(&c1,&c2); c3.show(); getch(); }
  • 33. What is this pointer?  Every object has a special pointer "this" which points to the object itself.  This pointer is accessible to all members of the class but not to any static members of the class.  Can be used to find the address of the object in which the function is a member.  Presence of this pointer is not included in the sizeof calculations.
  • 35. // Not using this pointer void MyClass::Print1() { cout << data << endl; } // Using this pointer void MyClass::Print2() { cout << "My address = " << this << endl; cout << this->data << endl; } int main() { MyClass a; a.Print1(); a.Print2(); // Size of doesn't include this pointer cout << sizeof(a) << endl; }
  • 36. OUTPUT: 100 My address = 0012FF88 100 4