Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Classes and Objects in C++
Syntax for class
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
};
In this member1 and member2 can be data variable or
member function
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Syntax for object
Method 1:
class class_name {
access_specifier_1:
member1;
access_specifier_2:
member2;
...
} objectname;
Method 2:
class_name object_name;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Syntax for input and output
Header file requires: #include <iostream.h>
using namespace std;
Input:
The C++ cin is an istream class predefined object.
It is linked to a standard input device, i.e., a keyboard.
To read input from a console, the cin is used in combination with the stream
extraction operator (>>).
Syntax:
cin >> height; other form of getting input : std::cin>>height
To read multiple inputs:
cin>>height>>width;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Syntax for input and output
Output:
The C++ cout is an ostream class predefined object.
It is linked to a standard output device, i.e., a screen.
To output the data, the cout is used in combination with the stream insertion
operator (<<).
Syntax:
cout<< height;
To read multiple inputs: std::cout<<height
cout<<height<<width;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Access Specifiers in C++
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
• An access specifier is one of the following three keywords:
private
public
protected.
• These specifiers modify the access rights for the members that follow them:
• private members of a class are accessible only from within other members of
the same class (or from their "friends").
• protected members are accessible from other members of the same class (or
from their "friends"), but also from members of their derived classes.
• Finally, public members are accessible from anywhere where the object is
visible.
Syntax for accessing members of a class
If the data member is public, then the data member can be easily accessed
using the direct member access (.) operator with the object of that class.
If, the data member is defined as private or protected, then we cannot access
the data variables directly.
Then we will have to create special public member functions to access, use or
initialize the private and protected data members.
The default access specifier in C++ is private.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Syntax for accessing Public members of a class
Public Specifier:
• obj.data_member = 10; // accessing public data member
• obj.memberfunc(); // accessing public member function
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Example Program
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
class Student
{
public:
int rollno;
string name;
};
int main()
{
Student A;
Student B;
// setting values for A object
A.rollno=1;
A.name="Adam";
// setting values for B object
B.rollno=2;
B.name="Bella";
cout <<"Name and Roll no of A is: "<< A.name << "-" << A.rollno;
cout <<"Name and Roll no of B is: "<< B.name << "-" << B.rollno;
}
Syntax for accessing Private members of a class
• Private members of the class cannot be accessed with the object directly
• To access, use and initialize the private data member you need to create
getter and setter functions, to get and set the value of the data member.
• The setter function will set the value passed as argument to the private data
member, and the getter function will return the value of the private data
member to be used. Both getter and setter function must be defined public.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Example Program
class Student
{ private: // private data member
int rollno;
public:
int getRollno()
{
return rollno;
}
void setRollno(int i)
{
rollno=i;
}
};
int main()
{
Student A;
A.setRollno(1); //Rollno initialized to 1
cout<< A.getRollno(); //Output will be 1 }
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Private Member Functions
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
A private member function can only be called by another function that is a member
of its class.
An object cannot invoke a private function using the dot operator.
Example:
Syntax for accessing Protected members of a class
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Protected data members, can be accessed directly using dot (.) operator
inside the subclass of the current class, for non-subclass we will have to
follow the steps same as to access private data member.
Method Definition Outside and Inside of a Class
class my_class
{
public:
// inside class definition of the function
void sum(int num1, int num2) // function header
{
cout << "The sum of the numbers is: "; // function body
cout << (num1 + num2) << "nn";
}
};
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Method Definition Outside and Inside of a Class
Outside:
void class_name :: function_name(arguments)
{
// function body
}
Example:
void my_class::sum(int num1, int num2) // function header
{
cout << "The sum of the numbers is: "; // function body
cout << (num1 + num2) << "nn";
}
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Classes and Objects- Example Program
• Write a C++ program to calculate the area of the rectangle using class
and objects.
// classes example
#include <iostream>
class CRectangle {
int width, height;
public:
void set_values (int,int);
int area ()
{
return (width*height);
}
};
void CRectangle::set_values (int a, int b)
{
width = a;
height = b;
}
int main ()
{
CRectangle rect;
rect.set_values (3,4);
cout << "area: " << rect.area();
return 0;
}
Array of Elements in a class
Arrays can be declared as the members of a class. The arrays can be declared
as private, public or protected members of the class.
Example:
class student {
int roll_no;
int marks[size];
public:
void getdata ();
void tot_marks ();
} ;
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Array of objects
In C++, an array of objects is a collection of objects of the same class type that
are stored in contiguous memory locations.
Since each item in the array is an instance of the class, each one's member
variables can have a unique value.
This makes it possible to manage and handle numerous objects by storing
them in a single data structure and giving them similar properties and
behaviours.
className arrayName[arraySize];
• arrayName is the name of the array of objects.
• arraySize is the number of objects in the array or the size of array, specified
as a constant expression.
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Programs using Class and Objects
Write a C++ program to find the factorial of a number using class and
object. Use private data members and public member functions such as
input, calculate and display to get the input, to calculate the factorial
value and to display the output. Define the functions outside the class.
Dr.J.saira banu , Associate Professor, SCOPE, VIT
University
Programs using Array of members in the class
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Write a C++ program to calculate the total marks secured by a student in 5
different subjects. Take the basic information of the student such as name,
roll number and the marks in the five different subjects. Use member
functions to get the data and to calculate the total marks.
Program using Array of objects
Dr.J.saira banu , Associate Professor, SCOPE, VIT University
Write a C++ program to calculate the total marks secured by a group of 5
students in 5 different subjects. Take the basic information of the student
such as name, roll number and the marks in the five different subjects. Use
member functions to get the data, to calculate the total marks and to display
the result.

3.Syntax.pptx for oops programing language

  • 1.
    Dr.J.saira banu ,Associate Professor, SCOPE, VIT University Classes and Objects in C++
  • 2.
    Syntax for class classclass_name { access_specifier_1: member1; access_specifier_2: member2; ... }; In this member1 and member2 can be data variable or member function Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 3.
    Syntax for object Method1: class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } objectname; Method 2: class_name object_name; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 4.
    Syntax for inputand output Header file requires: #include <iostream.h> using namespace std; Input: The C++ cin is an istream class predefined object. It is linked to a standard input device, i.e., a keyboard. To read input from a console, the cin is used in combination with the stream extraction operator (>>). Syntax: cin >> height; other form of getting input : std::cin>>height To read multiple inputs: cin>>height>>width; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 5.
    Syntax for inputand output Output: The C++ cout is an ostream class predefined object. It is linked to a standard output device, i.e., a screen. To output the data, the cout is used in combination with the stream insertion operator (<<). Syntax: cout<< height; To read multiple inputs: std::cout<<height cout<<height<<width; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 6.
    Access Specifiers inC++ Dr.J.saira banu , Associate Professor, SCOPE, VIT University • An access specifier is one of the following three keywords: private public protected. • These specifiers modify the access rights for the members that follow them: • private members of a class are accessible only from within other members of the same class (or from their "friends"). • protected members are accessible from other members of the same class (or from their "friends"), but also from members of their derived classes. • Finally, public members are accessible from anywhere where the object is visible.
  • 7.
    Syntax for accessingmembers of a class If the data member is public, then the data member can be easily accessed using the direct member access (.) operator with the object of that class. If, the data member is defined as private or protected, then we cannot access the data variables directly. Then we will have to create special public member functions to access, use or initialize the private and protected data members. The default access specifier in C++ is private. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 8.
    Syntax for accessingPublic members of a class Public Specifier: • obj.data_member = 10; // accessing public data member • obj.memberfunc(); // accessing public member function Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 9.
    Example Program Dr.J.saira banu, Associate Professor, SCOPE, VIT University class Student { public: int rollno; string name; }; int main() { Student A; Student B; // setting values for A object A.rollno=1; A.name="Adam"; // setting values for B object B.rollno=2; B.name="Bella"; cout <<"Name and Roll no of A is: "<< A.name << "-" << A.rollno; cout <<"Name and Roll no of B is: "<< B.name << "-" << B.rollno; }
  • 10.
    Syntax for accessingPrivate members of a class • Private members of the class cannot be accessed with the object directly • To access, use and initialize the private data member you need to create getter and setter functions, to get and set the value of the data member. • The setter function will set the value passed as argument to the private data member, and the getter function will return the value of the private data member to be used. Both getter and setter function must be defined public. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 11.
    Example Program class Student {private: // private data member int rollno; public: int getRollno() { return rollno; } void setRollno(int i) { rollno=i; } }; int main() { Student A; A.setRollno(1); //Rollno initialized to 1 cout<< A.getRollno(); //Output will be 1 } Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 12.
    Private Member Functions Dr.J.sairabanu , Associate Professor, SCOPE, VIT University A private member function can only be called by another function that is a member of its class. An object cannot invoke a private function using the dot operator. Example:
  • 13.
    Syntax for accessingProtected members of a class Dr.J.saira banu , Associate Professor, SCOPE, VIT University Protected data members, can be accessed directly using dot (.) operator inside the subclass of the current class, for non-subclass we will have to follow the steps same as to access private data member.
  • 14.
    Method Definition Outsideand Inside of a Class class my_class { public: // inside class definition of the function void sum(int num1, int num2) // function header { cout << "The sum of the numbers is: "; // function body cout << (num1 + num2) << "nn"; } }; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 15.
    Method Definition Outsideand Inside of a Class Outside: void class_name :: function_name(arguments) { // function body } Example: void my_class::sum(int num1, int num2) // function header { cout << "The sum of the numbers is: "; // function body cout << (num1 + num2) << "nn"; } Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 16.
    Classes and Objects-Example Program • Write a C++ program to calculate the area of the rectangle using class and objects. // classes example #include <iostream> class CRectangle { int width, height; public: void set_values (int,int); int area () { return (width*height); } }; void CRectangle::set_values (int a, int b) { width = a; height = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0; }
  • 17.
    Array of Elementsin a class Arrays can be declared as the members of a class. The arrays can be declared as private, public or protected members of the class. Example: class student { int roll_no; int marks[size]; public: void getdata (); void tot_marks (); } ; Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 18.
    Array of objects InC++, an array of objects is a collection of objects of the same class type that are stored in contiguous memory locations. Since each item in the array is an instance of the class, each one's member variables can have a unique value. This makes it possible to manage and handle numerous objects by storing them in a single data structure and giving them similar properties and behaviours. className arrayName[arraySize]; • arrayName is the name of the array of objects. • arraySize is the number of objects in the array or the size of array, specified as a constant expression. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 19.
    Programs using Classand Objects Write a C++ program to find the factorial of a number using class and object. Use private data members and public member functions such as input, calculate and display to get the input, to calculate the factorial value and to display the output. Define the functions outside the class. Dr.J.saira banu , Associate Professor, SCOPE, VIT University
  • 20.
    Programs using Arrayof members in the class Dr.J.saira banu , Associate Professor, SCOPE, VIT University Write a C++ program to calculate the total marks secured by a student in 5 different subjects. Take the basic information of the student such as name, roll number and the marks in the five different subjects. Use member functions to get the data and to calculate the total marks.
  • 21.
    Program using Arrayof objects Dr.J.saira banu , Associate Professor, SCOPE, VIT University Write a C++ program to calculate the total marks secured by a group of 5 students in 5 different subjects. Take the basic information of the student such as name, roll number and the marks in the five different subjects. Use member functions to get the data, to calculate the total marks and to display the result.