KEMBAR78
classes and objects_oops programming.pptx
CLASSES AND OBJECTS
Introduction to classes
Important feature of C++
Bjarne Stroustrup named ‘C with classes’
Features provided
Abstraction
Encapsulation
Inheritance
Polymorphism
Need for Class
Way of binding the data and its associated
functions together
Used to create user defined datatype
Declaration of class
Class is defined using keyword class followed
by the name of the class
General Form
class classname
{
private:
variable declaration;
function declaration;
protected:
variable declaration;
function declaration;
public:
variable declaration;
function declaration;
};
Class Access Specifiers
Public
Private
protected
Definition of class members
Example
class result
{
private:
char name[10];
int rollno,mark1,mark2,mark3,total;
public:
void accept();
void display();
};
Data
members
Member
functions
Defining methods of a class
Inside the class definition(Inline member
function)
Outside the class definition(outline/non-inline
member function)
Without defining function, class definition
will become incomplete
Defining outside the class uses Scope
Resolution Operator(::)
Syntax for defining outside the
class
returntype classname::functionname(list)
{
function definition
}
Example:
void add::display()
{
function definition
}
Creating objects
To make use of a class, the variables of that
class type have to be declared
The class variables are called as objects(instance
of class)
Example:
student s;
instance of class student
Methods for creating objects
Global object
Declared outside all the functions or placed
immediately after the closing brace
Local object
Declared within a function and it cannot be
accessed from outside the function
Methods for creating objects -
Example
class add
{
------
------
}a1; //global object
add a2; //global object
int main()
{
add a3; //local object
--------
}
Memory allocation of objects
Memory space is allocated for data members when the
objects are created
No memory is allocated for member functions since all
the objects use the same member functions
Memory allocation of objects-
Example
#include<iostream>
using namespace std;
class add
{
int num1,num2,result;
public:
void read()
{
cout<<“Enter number1:”;
cin>>num1;
cout<<“Enter number2:”;
cin>>num2;
}
void sum()
{
result=num1+num2;
}
};
void add::display()
{
cout<<“Result=“<<num1<<“+”<<num2<
<“=“<<result<<endl;
}
int main()
{
add obj1,obj2;
obj1.read();
obj1.sum();
obj1.display();
obj2.read();
obj2.sum();
obj2.display();
getch();
return 0;
}

classes and objects_oops programming.pptx

  • 1.
  • 2.
    Introduction to classes Importantfeature of C++ Bjarne Stroustrup named ‘C with classes’ Features provided Abstraction Encapsulation Inheritance Polymorphism
  • 3.
    Need for Class Wayof binding the data and its associated functions together Used to create user defined datatype
  • 4.
    Declaration of class Classis defined using keyword class followed by the name of the class
  • 5.
    General Form class classname { private: variabledeclaration; function declaration; protected: variable declaration; function declaration; public: variable declaration; function declaration; };
  • 6.
  • 7.
    Definition of classmembers Example class result { private: char name[10]; int rollno,mark1,mark2,mark3,total; public: void accept(); void display(); }; Data members Member functions
  • 8.
    Defining methods ofa class Inside the class definition(Inline member function) Outside the class definition(outline/non-inline member function) Without defining function, class definition will become incomplete Defining outside the class uses Scope Resolution Operator(::)
  • 9.
    Syntax for definingoutside the class returntype classname::functionname(list) { function definition } Example: void add::display() { function definition }
  • 10.
    Creating objects To makeuse of a class, the variables of that class type have to be declared The class variables are called as objects(instance of class) Example: student s; instance of class student
  • 11.
    Methods for creatingobjects Global object Declared outside all the functions or placed immediately after the closing brace Local object Declared within a function and it cannot be accessed from outside the function
  • 12.
    Methods for creatingobjects - Example class add { ------ ------ }a1; //global object add a2; //global object int main() { add a3; //local object -------- }
  • 13.
    Memory allocation ofobjects Memory space is allocated for data members when the objects are created No memory is allocated for member functions since all the objects use the same member functions
  • 14.
    Memory allocation ofobjects- Example #include<iostream> using namespace std; class add { int num1,num2,result; public: void read() { cout<<“Enter number1:”; cin>>num1; cout<<“Enter number2:”; cin>>num2; } void sum() { result=num1+num2; } }; void add::display() { cout<<“Result=“<<num1<<“+”<<num2< <“=“<<result<<endl; } int main() { add obj1,obj2; obj1.read(); obj1.sum(); obj1.display(); obj2.read(); obj2.sum(); obj2.display(); getch(); return 0; }