Introduction to Class
Dardina Tasmere
Lecturer, Dept. of CSE, BAUET
Class
• The main purpose of C++ programming is to add object orientation to the C
programming language
• Classes are the central feature of C++ that supports object-oriented programming
and are often called user-defined types.
• When related data and functions are kept under a class, it helps to visualize the
complex problem efficiently and effectively.
Class
 A class is the collection of related data and function under a single name.
 A C++ program can have any number of classes.
 Class are also just the specification for objects and object bears the property of that class.
 A Class is a blueprint for objects.
Class: How to declare?
• A class definition starts with the keyword class followed by the class name;
• and the class body, enclosed by a pair of curly braces.
• A class definition must be followed either by a semicolon or a list of
declarations.
Access
Specifiers
Class: Example
Functions and variables
declared inside a class
declaration are said to be
members of this class.
Looks like structure?
Program example with structure and class
Object of class Box
Class: Example
Object definition?
C++ Objects
• When class is defined, only specification for the object is defined.
• Object has same relationship to class as variable has with the data type.
• Objects can be defined in similarly way as structure is defined.
• An object is an instantiation of a class. In terms of variables, a class would be the
type, and an object would be the variable.
• A class provides the blueprints for objects, so basically an object is created from a
class.
• We declare objects of a class with exactly the same sort of declaration that we
declare variables of basic types.
Following statements declare two objects of class Box:
Box Box1; // Declare Box1 of type Box
Box Box2; // Declare Box2 of type Box
Access Specifiers: private
• Variables or functions declared after access specifier private accessible only to
member functions of the class for which they’re declared.
• The default access for class members is private.
• Declaring data members with access specifier private is known as data hiding.
(encapsulation)
Access Specifiers: public
• Variables or functions declared after access specifier public accessible both by
other members of the class for which they’re declared and other part of the
program that contains the class.
• The default access for class members is private.
• Declaring data members with access specifier private is known as data hiding.
(encapsulation)
Public, Private
Private variable of class Box.
Public variable of class Box.
No problem, you can access a public variable through objects of that class.
Problem, private variables can access only through member functions of that class..
Class with member function
Definition
Function that are declared to be part of a class are called member
function of that class.
Example
ob
l w h Print_volume
UK UK UK
ob2
l w h Print_volume
UK UK UK
Each object of a class has
its own copy of every
variable declared within
the class.
ob
l w h Print_volume
UK UK UK
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
UK UK UK
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 UK UK
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 UK
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 10
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 10
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 10
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 10
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 10
ob2
l w h Print_volume
UK UK UK
ob
l w h Print_volume
5 10 10
ob2
l w h Print_volume
UK UK UK
500
Member function with Scope resolution operator
( :: )
1. Return type of the function.
2. Class name
3. Scope resolution operator ::
4. Function name with
parameter list.
5. Function body between {}
How to do that?
return_type class_name :: function_name()
{
//function body.
}
Member function with Scope resolution operator
( :: )
How to do that?
return_type class_name :: function_name()
{
//function body.
}
Member function with Scope resolution operator
Dealing with private data
It is common practice to declare all variables as private and functions as
public.
Example
Explanation
In the example l is private.
This means that only member functions of Box class can access it
directly.
get_l is a public function of class Box. And as being a function of
class Box, get_l can access the private data l.
If we try to access a private member of class from any other part of
program (that is not a part of that class) then a compile-time error
will result.
ERROR!!
#include<iostream>
using namespace std;
int main(){
Box ob,ob2;
ob.l=10;//can’t access private member
cout<<ob.get_l()<<endl;
return 0;
}

Introduction to Class a deep analysisadfas

  • 1.
    Introduction to Class DardinaTasmere Lecturer, Dept. of CSE, BAUET
  • 2.
    Class • The mainpurpose of C++ programming is to add object orientation to the C programming language • Classes are the central feature of C++ that supports object-oriented programming and are often called user-defined types. • When related data and functions are kept under a class, it helps to visualize the complex problem efficiently and effectively.
  • 3.
    Class  A classis the collection of related data and function under a single name.  A C++ program can have any number of classes.  Class are also just the specification for objects and object bears the property of that class.  A Class is a blueprint for objects.
  • 4.
    Class: How todeclare? • A class definition starts with the keyword class followed by the class name; • and the class body, enclosed by a pair of curly braces. • A class definition must be followed either by a semicolon or a list of declarations. Access Specifiers
  • 5.
    Class: Example Functions andvariables declared inside a class declaration are said to be members of this class. Looks like structure? Program example with structure and class Object of class Box
  • 6.
  • 7.
    C++ Objects • Whenclass is defined, only specification for the object is defined. • Object has same relationship to class as variable has with the data type. • Objects can be defined in similarly way as structure is defined. • An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. • A class provides the blueprints for objects, so basically an object is created from a class. • We declare objects of a class with exactly the same sort of declaration that we declare variables of basic types. Following statements declare two objects of class Box: Box Box1; // Declare Box1 of type Box Box Box2; // Declare Box2 of type Box
  • 8.
    Access Specifiers: private •Variables or functions declared after access specifier private accessible only to member functions of the class for which they’re declared. • The default access for class members is private. • Declaring data members with access specifier private is known as data hiding. (encapsulation)
  • 9.
    Access Specifiers: public •Variables or functions declared after access specifier public accessible both by other members of the class for which they’re declared and other part of the program that contains the class. • The default access for class members is private. • Declaring data members with access specifier private is known as data hiding. (encapsulation)
  • 10.
    Public, Private Private variableof class Box. Public variable of class Box. No problem, you can access a public variable through objects of that class. Problem, private variables can access only through member functions of that class..
  • 11.
    Class with memberfunction Definition Function that are declared to be part of a class are called member function of that class. Example
  • 14.
    ob l w hPrint_volume UK UK UK ob2 l w h Print_volume UK UK UK Each object of a class has its own copy of every variable declared within the class.
  • 15.
    ob l w hPrint_volume UK UK UK ob2 l w h Print_volume UK UK UK
  • 16.
    ob l w hPrint_volume UK UK UK ob2 l w h Print_volume UK UK UK
  • 17.
    ob l w hPrint_volume 5 UK UK ob2 l w h Print_volume UK UK UK
  • 18.
    ob l w hPrint_volume 5 10 UK ob2 l w h Print_volume UK UK UK
  • 19.
    ob l w hPrint_volume 5 10 10 ob2 l w h Print_volume UK UK UK
  • 20.
    ob l w hPrint_volume 5 10 10 ob2 l w h Print_volume UK UK UK
  • 21.
    ob l w hPrint_volume 5 10 10 ob2 l w h Print_volume UK UK UK
  • 22.
    ob l w hPrint_volume 5 10 10 ob2 l w h Print_volume UK UK UK
  • 23.
    ob l w hPrint_volume 5 10 10 ob2 l w h Print_volume UK UK UK
  • 24.
    ob l w hPrint_volume 5 10 10 ob2 l w h Print_volume UK UK UK 500
  • 25.
    Member function withScope resolution operator ( :: ) 1. Return type of the function. 2. Class name 3. Scope resolution operator :: 4. Function name with parameter list. 5. Function body between {} How to do that? return_type class_name :: function_name() { //function body. }
  • 26.
    Member function withScope resolution operator ( :: ) How to do that? return_type class_name :: function_name() { //function body. }
  • 27.
    Member function withScope resolution operator
  • 28.
    Dealing with privatedata It is common practice to declare all variables as private and functions as public. Example
  • 31.
    Explanation In the examplel is private. This means that only member functions of Box class can access it directly. get_l is a public function of class Box. And as being a function of class Box, get_l can access the private data l. If we try to access a private member of class from any other part of program (that is not a part of that class) then a compile-time error will result.
  • 32.
    ERROR!! #include<iostream> using namespace std; intmain(){ Box ob,ob2; ob.l=10;//can’t access private member cout<<ob.get_l()<<endl; return 0; }

Editor's Notes

  • #5 Program example with structure and class