SlideShare a Scribd company logo
Classes and Objects
Introduction
• Object-oriented programming (OOP)
– Encapsulation: encapsulates data (attributes) and
functions (behaviour) into packages called classes
– Information hiding : implementation details are hidden
within the classes themselves
• Classes
– Classes are the standard unit of programming
– Aclass is like a blueprint – reusable
– Objects are instantiated (created) from the class
– For example, a house is an instance of a “blueprint
class”
Implementing a Time Abstract Data Type
with a Class
• Classes
– Model objects that have attributes (data
members) and behaviours (member functions)
– Defined using keyword class
– Have a body delineated with braces ({ and })
– Class definitions terminate with a semicolon
– Example:
C++ppt. Classs and object, class and object
1 class Time {
2 public:
3 Time();
4 void setTime( int, int, int );
5 void printMilitary();
6 void printStandard();
7 private:
Public: and Private: are
member-access specifiers.
setTime, printMilitary, and
printStandard are member
functions.
Time is the constructor.
8 int hour; // 0 - 23
9 int minute; // 0 - 59
10 int second; // 0 - 59 hour, minute, and second
11 }; are data members.
Implementing a Time Abstract Data Type
with a Class
• Member access specifiers
– Classes can limit the access to their member functions and data
– The three types of access a class can grant are:
• Public — Accessible wherever the program has access to an object of
the class
• private — Accessible only to member functions of the class
• Protected — Similar to private and discussed later
• Constructor
– Special member function that initialises the data members of a
class object
– Cannot return values
– Have the same name as the class
Objects
• Class definition and declaration
– Once a class has been defined, it can be used as
a type in object, array and pointer declarations
– Example:
Time sunset, // object of type Time
// array of Time objects
// pointer to a Time object
// reference to a Time object
arrayOfTimes[ 5 ],
ime,
= sunset;
*pointerToT
&dinnerTime
Note: The class name
becomes the new type
specifier.
// C++ program to demonstrate accessing of data members
class abc {
//Access specifier
public:
// Data Members
string abcName;
// Member Functions()
void printname() { cout << "AbcName is:" << geekname; }
};
int main()
{
// Declare an object of class geeks
abc obj1;
// accessing data member
obj1.abcName = "Abhi";
// accessing member function
obj1.printname();
return 0;
}
class abc
{
public:
string abcName;
int id;
// printname is not defined inside class definition
void printname();
// printid is defined inside class definition
void printid()
{
cout <<"abc id is: "<<id;
}
};
// Definition of printname using scope resolution operator ::
void Geeks::printname()
{
cout <<"abcName is: "<<abcName;
}
int main() {
Geeks obj1;
obj1.abcName = "xyz";
obj1.id=15;
// call printname()
obj1.printname();
cout << endl;
// call printid()
obj1.printid();
return 0;
}
Implementing with a Class
• Binary scope resolution operator (::)
– Combines the class name with the member function
name
– Different classes can have member functions with the
same name
• Format for defining member functions
ReturnType ClassName::MemberFunctionName( ){
…
}
• If a member function is defined inside the class
– Scope resolution operator and class name are not
needed
– Defining a function outside a class does not change
it being public or private
• Classes encourage software reuse
– Inheritance allows new classes to be derived from
old ones
Implementing with a Class
Class Scope and Accessing Class
Members
• Class scope
– Data members and member functions
• Inside a scope
– Members accessible by all member functions
• Referenced by name
• Outside a scope
– Members are referenced through handles
• An object name, a reference to an object or a pointer to an object
Class Scope and Accessing Class
Members
• Function scope
– Variables only known to function they are defined in
– Variables are destroyed after function completion
• Accessing class members
– Same as structs
– Dot (.) for objects and arrow (->) for pointers
– Example:
• t.hour is the hour element of t
• TimePtr->hour is the hour element
Controlling Access to Members
• public
– Presents clients with a view of the services the class
provides (interface)
– Data and member functions are accessible
• private
– Default access mode
– Data only accessible to member functions and friends
– private members only accessible through the
public class interface using public member
functions
// To demonstrate public
#include<iostream>
using namespace std;
// class definition
class Circle
{
public:
double radius;
double compute_area()
{
return 3.14*radius*radius;
}
};
// main function
int main()
{
Circle obj;
// accessing public datamember outside class
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "n";
cout << "Area is: " << obj.compute_area();
return 0;
}
// To demonstrate private
#include<iostream>
using namespace std;
class Circle
{
// private data member
private:
double radius;
// public member function
public:
double compute_area()
{ // member function can access private
// data member radius
return 3.14*radius*radius;
}
};
// main function
void main()
{
// creating object of the class
Circle obj;
// trying to access private data member
// directly outside the class
obj.radius = 1.5;
cout << "Area is:" << obj.compute_area();
}
// C++ program to demonstrate protected
class Parent
{
// protected data members
protected:
int id_protected;
};
// sub class or derived class from public base class
class Child : public Parent
{
public:
void setId(int id)
{
// Child class is able to access the inherited
// protected data members of base class
id_protected = id;
}
void displayId()
{
cout << "id_protected is: " << id_protected << endl;
}
};
void main() {
Child obj1;
obj1.setId(81);
obj1.displayId();
}
Access Functions and Utility Functions
• Utility functions
– private functions that support the operation of public
functions
– Not intended to be used directly by clients
• Access functions
– public functions that read/display data or check
conditions
– Allow public functions to check private data
• Following example
– Program to take in monthly sales and output the total
– Implementation not shown, only access functions
Class definition
class class_name {
public:
constructor and destructor
member functions
private:
data members
};
InitialiSing Class Objects: Constructors
• Constructors
– InitialiSe class members
– Same name as the class
– No return type
– Member variables can be initialised by the constructor or
set afterwards
• Passing arguments to a constructor
– When an object of a class is declared, initialisers can be
provided
– Format of declaration with initialisers:
Class-type ObjectName( value1, value2,…);
– Default arguments may also be specified in the
constructor prototype
// defining the constructor within the class
#include <iostream>
using namespace std;
class student {
int rno;
char name[50];
double fee;
public:
// constructor
student()
{
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}
void display()
{
cout << endl << rno << "t" << name << "t" << fee;
}
};
int main()
{
student s; // constructor gets called automatically when
// we create the object of the class
s.display();
return 0;
}
// defining the constructor outside the class
#include <iostream>
using namespace std;
class student {
int rno;
char name[50];
double fee;
public:
// constructor declaration only
student();
void display();
};
// outside definition of constructor
student::student()
{
cout << "Enter the RollNo:";
cin >> rno;
cout << "Enter the Name:";
cin >> name;
cout << "Enter the Fee:";
cin >> fee;
}
void student::display()
{
cout << endl << rno << "t" << name << "t" << fee;
}
void main()
{
student s;
s.display();
}
Software Reusability
• Software resusability
– Implementation of useful classes
– Class libraries exist to promote reusability
• Allows for construction of programs from existing, well-
defined, carefully tested, well-documented, portable, widely
available components
– Speeds development of powerful, high-quality software
Thank You

More Related Content

PPT
Classes and objects constructor and destructor
PPT
Classes, objects and methods
PPT
C++ Programming Course
PPT
Ccourse 140618093931-phpapp02
PDF
Object Oriented Programming using C++ - Part 2
PPTX
Classes and objects
PPT
UNIT I (1).ppt
PPT
UNIT I (1).ppt
Classes and objects constructor and destructor
Classes, objects and methods
C++ Programming Course
Ccourse 140618093931-phpapp02
Object Oriented Programming using C++ - Part 2
Classes and objects
UNIT I (1).ppt
UNIT I (1).ppt

Similar to C++ppt. Classs and object, class and object (20)

PPTX
OBJECT ORIENTED PROGRAMING IN C++
PPTX
object oriented programming-classes and objects.pptx
PPTX
Class and object
PPT
C++ Classes Tutorials.ppt
PPTX
C# classes objects
PPTX
Chapter 2 OOP using C++ (Introduction).pptx
PPSX
Object oriented programming 2
PPT
Classes and objects
PDF
PPTX
C++ & Data Structure - Unit - first.pptx
PDF
CLASSES and OBJECTS in C++ detailed explanation
PPTX
Classes, Inheritance ,Packages & Interfaces.pptx
PPT
classes data type for Btech students.ppt
PDF
chapter-7-classes-and-objects.pdf
PPTX
Introduction to Fundamental of Class.pptx
PPTX
oop lecture 3
PPTX
OOPs & C++ UNIT 3
PPTX
Presentation on class and object in Object Oriented programming.
PPTX
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
OBJECT ORIENTED PROGRAMING IN C++
object oriented programming-classes and objects.pptx
Class and object
C++ Classes Tutorials.ppt
C# classes objects
Chapter 2 OOP using C++ (Introduction).pptx
Object oriented programming 2
Classes and objects
C++ & Data Structure - Unit - first.pptx
CLASSES and OBJECTS in C++ detailed explanation
Classes, Inheritance ,Packages & Interfaces.pptx
classes data type for Btech students.ppt
chapter-7-classes-and-objects.pdf
Introduction to Fundamental of Class.pptx
oop lecture 3
OOPs & C++ UNIT 3
Presentation on class and object in Object Oriented programming.
C++ Intro C++ Intro C++ Intro C++ Intro C++ Intro
Ad

Recently uploaded (20)

PPTX
climate analysis of Dhaka ,Banglades.pptx
PPTX
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
PDF
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
PPTX
Introduction to Knowledge Engineering Part 1
PDF
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
PDF
Galatica Smart Energy Infrastructure Startup Pitch Deck
PPTX
Acceptance and paychological effects of mandatory extra coach I classes.pptx
PDF
Clinical guidelines as a resource for EBP(1).pdf
PDF
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
PPT
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
PPTX
Business Ppt On Nestle.pptx huunnnhhgfvu
PPT
Miokarditis (Inflamasi pada Otot Jantung)
PPTX
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
PPTX
Introduction-to-Cloud-ComputingFinal.pptx
PPTX
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
PPTX
Supervised vs unsupervised machine learning algorithms
PDF
Lecture1 pattern recognition............
PPTX
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
PPTX
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
climate analysis of Dhaka ,Banglades.pptx
MODULE 8 - DISASTER risk PREPAREDNESS.pptx
168300704-gasification-ppt.pdfhghhhsjsjhsuxush
Introduction to Knowledge Engineering Part 1
Recruitment and Placement PPT.pdfbjfibjdfbjfobj
Galatica Smart Energy Infrastructure Startup Pitch Deck
Acceptance and paychological effects of mandatory extra coach I classes.pptx
Clinical guidelines as a resource for EBP(1).pdf
22.Patil - Early prediction of Alzheimer’s disease using convolutional neural...
Chapter 2 METAL FORMINGhhhhhhhjjjjmmmmmmmmm
Business Ppt On Nestle.pptx huunnnhhgfvu
Miokarditis (Inflamasi pada Otot Jantung)
advance b rammar.pptxfdgdfgdfsgdfgsdgfdfgdfgsdfgdfgdfg
Introduction-to-Cloud-ComputingFinal.pptx
Introduction to Firewall Analytics - Interfirewall and Transfirewall.pptx
Supervised vs unsupervised machine learning algorithms
Lecture1 pattern recognition............
CEE 2 REPORT G7.pptxbdbshjdgsgjgsjfiuhsd
The THESIS FINAL-DEFENSE-PRESENTATION.pptx
Ad

C++ppt. Classs and object, class and object

  • 2. Introduction • Object-oriented programming (OOP) – Encapsulation: encapsulates data (attributes) and functions (behaviour) into packages called classes – Information hiding : implementation details are hidden within the classes themselves • Classes – Classes are the standard unit of programming – Aclass is like a blueprint – reusable – Objects are instantiated (created) from the class – For example, a house is an instance of a “blueprint class”
  • 3. Implementing a Time Abstract Data Type with a Class • Classes – Model objects that have attributes (data members) and behaviours (member functions) – Defined using keyword class – Have a body delineated with braces ({ and }) – Class definitions terminate with a semicolon – Example:
  • 5. 1 class Time { 2 public: 3 Time(); 4 void setTime( int, int, int ); 5 void printMilitary(); 6 void printStandard(); 7 private: Public: and Private: are member-access specifiers. setTime, printMilitary, and printStandard are member functions. Time is the constructor. 8 int hour; // 0 - 23 9 int minute; // 0 - 59 10 int second; // 0 - 59 hour, minute, and second 11 }; are data members.
  • 6. Implementing a Time Abstract Data Type with a Class • Member access specifiers – Classes can limit the access to their member functions and data – The three types of access a class can grant are: • Public — Accessible wherever the program has access to an object of the class • private — Accessible only to member functions of the class • Protected — Similar to private and discussed later • Constructor – Special member function that initialises the data members of a class object – Cannot return values – Have the same name as the class
  • 7. Objects • Class definition and declaration – Once a class has been defined, it can be used as a type in object, array and pointer declarations – Example: Time sunset, // object of type Time // array of Time objects // pointer to a Time object // reference to a Time object arrayOfTimes[ 5 ], ime, = sunset; *pointerToT &dinnerTime Note: The class name becomes the new type specifier.
  • 8. // C++ program to demonstrate accessing of data members class abc { //Access specifier public: // Data Members string abcName; // Member Functions() void printname() { cout << "AbcName is:" << geekname; } }; int main() { // Declare an object of class geeks abc obj1; // accessing data member obj1.abcName = "Abhi"; // accessing member function obj1.printname(); return 0; }
  • 9. class abc { public: string abcName; int id; // printname is not defined inside class definition void printname(); // printid is defined inside class definition void printid() { cout <<"abc id is: "<<id; } }; // Definition of printname using scope resolution operator :: void Geeks::printname() { cout <<"abcName is: "<<abcName; } int main() { Geeks obj1; obj1.abcName = "xyz"; obj1.id=15; // call printname() obj1.printname(); cout << endl; // call printid() obj1.printid(); return 0; }
  • 10. Implementing with a Class • Binary scope resolution operator (::) – Combines the class name with the member function name – Different classes can have member functions with the same name • Format for defining member functions ReturnType ClassName::MemberFunctionName( ){ … }
  • 11. • If a member function is defined inside the class – Scope resolution operator and class name are not needed – Defining a function outside a class does not change it being public or private • Classes encourage software reuse – Inheritance allows new classes to be derived from old ones Implementing with a Class
  • 12. Class Scope and Accessing Class Members • Class scope – Data members and member functions • Inside a scope – Members accessible by all member functions • Referenced by name • Outside a scope – Members are referenced through handles • An object name, a reference to an object or a pointer to an object
  • 13. Class Scope and Accessing Class Members • Function scope – Variables only known to function they are defined in – Variables are destroyed after function completion • Accessing class members – Same as structs – Dot (.) for objects and arrow (->) for pointers – Example: • t.hour is the hour element of t • TimePtr->hour is the hour element
  • 14. Controlling Access to Members • public – Presents clients with a view of the services the class provides (interface) – Data and member functions are accessible • private – Default access mode – Data only accessible to member functions and friends – private members only accessible through the public class interface using public member functions
  • 15. // To demonstrate public #include<iostream> using namespace std; // class definition class Circle { public: double radius; double compute_area() { return 3.14*radius*radius; } }; // main function int main() { Circle obj; // accessing public datamember outside class obj.radius = 5.5; cout << "Radius is: " << obj.radius << "n"; cout << "Area is: " << obj.compute_area(); return 0; }
  • 16. // To demonstrate private #include<iostream> using namespace std; class Circle { // private data member private: double radius; // public member function public: double compute_area() { // member function can access private // data member radius return 3.14*radius*radius; } }; // main function void main() { // creating object of the class Circle obj; // trying to access private data member // directly outside the class obj.radius = 1.5; cout << "Area is:" << obj.compute_area(); }
  • 17. // C++ program to demonstrate protected class Parent { // protected data members protected: int id_protected; }; // sub class or derived class from public base class class Child : public Parent { public: void setId(int id) { // Child class is able to access the inherited // protected data members of base class id_protected = id; } void displayId() { cout << "id_protected is: " << id_protected << endl; } }; void main() { Child obj1; obj1.setId(81); obj1.displayId(); }
  • 18. Access Functions and Utility Functions • Utility functions – private functions that support the operation of public functions – Not intended to be used directly by clients • Access functions – public functions that read/display data or check conditions – Allow public functions to check private data • Following example – Program to take in monthly sales and output the total – Implementation not shown, only access functions
  • 19. Class definition class class_name { public: constructor and destructor member functions private: data members };
  • 20. InitialiSing Class Objects: Constructors • Constructors – InitialiSe class members – Same name as the class – No return type – Member variables can be initialised by the constructor or set afterwards • Passing arguments to a constructor – When an object of a class is declared, initialisers can be provided – Format of declaration with initialisers: Class-type ObjectName( value1, value2,…); – Default arguments may also be specified in the constructor prototype
  • 21. // defining the constructor within the class #include <iostream> using namespace std; class student { int rno; char name[50]; double fee; public: // constructor student() { cout << "Enter the RollNo:"; cin >> rno; cout << "Enter the Name:"; cin >> name; cout << "Enter the Fee:"; cin >> fee; } void display() { cout << endl << rno << "t" << name << "t" << fee; } }; int main() { student s; // constructor gets called automatically when // we create the object of the class s.display(); return 0; }
  • 22. // defining the constructor outside the class #include <iostream> using namespace std; class student { int rno; char name[50]; double fee; public: // constructor declaration only student(); void display(); }; // outside definition of constructor student::student() { cout << "Enter the RollNo:"; cin >> rno; cout << "Enter the Name:"; cin >> name; cout << "Enter the Fee:"; cin >> fee; } void student::display() { cout << endl << rno << "t" << name << "t" << fee; } void main() { student s; s.display(); }
  • 23. Software Reusability • Software resusability – Implementation of useful classes – Class libraries exist to promote reusability • Allows for construction of programs from existing, well- defined, carefully tested, well-documented, portable, widely available components – Speeds development of powerful, high-quality software