OOP 2
OOP 2
Classes
Classes
the class declaration describes the type and scope of its members.
The class function definition describes how the class functions are implemented.
Syntax:-
class class-name
private:
variable declarations;
function declaration ;
public:
variable declarations;
function declaration;
};
The members that have been declared as private can be accessed only from with in the class.
On the other hand , public members can be accessed from outside the class also.
The data hiding is the key feature of oops.
The use of keywords private is optional by default, the members of a class are private.
The variables declared inside the class are known as data members and the functions are known
as members functions.
Only the member functions can have access to the private data members and private functions.
However, the public members can be accessed from the outside the class.
The binding of data and functions together into a single class type variable is referred to as
encapsulation.
Syntax:-
class item
{
int member;
float cost;
public:
void getdata (int a ,float b);
void putdata (void);
The class item contains two data members and two function members,
the data members are private by default while both the functions are
public by declaration.
The function getdata() can be used to assign values to the member
variables member and cost, and putdata() for displaying their values .
These functions provide the only access to the data members from
outside the class.
Simple Program For Declaring Classes
#include <iostream>
using namespace std;
class smalljob //define a class
{
private:
int somedata; //class data
public:
void setdata(int d) //member function to set data
{
somedata = d;
}
void showdata() //member function to display data
{
cout << "Data is = "<< somedata << endl;
}
};
int main()
{
smalljob s1, s2; //define two objects of class smallobj
s1.setdata(1066); //call member function to set data
s2.setdata(1776);
s1.showdata(); //call member function to display data
s2.showdata();
return 0;
}
The O/P
The Data Is = 1066
The Data Is = 1776
The class smalljob defined in this program contains one data item and two member functions.
The two member functions provide the only access to the data item from outside the class.
The first member function sets the data item to a value, and the second displays the value.
Placing data and functions together into a single entity is a central idea in object-oriented
programming.
This is shown in Figure 6.1
Classes and Objects
An object has the same relationship to a class that a variable has to a data type.
An object is said to be an example of a class,
In smalljob , the class—whose name is smalljob —is defined in
the first part of the program.
Later, in main(), we define two objects—s1 and s2—that are
example of that class.
Each of the two objects is given a value, and each displays its value.
Here’s the output of the program:
Data is 1066 ← object s1 displayed this
Data is 1776 ← object s2 displayed this
We’ll begin by looking in detail at the first part of the program—the
definition of the class smalljob .
Later we’ll focus on what main() does with objects of this class
Defining the Class
Here’s the definition (sometimes called a specifier) for the class smalljob , copied from the
SMALLJOB listing:
private:
public:
example.
Like a structure, the body of the class is delimited by braces and terminated by a
semicolon.
Remember that data structures such as structs and classes end with a semicolon,
while control structures such as functions and loops do not end with a semicolon.)
Private And Public
The body of the class contains two unfamiliar keywords: private and public.
This term does not refer to the activities of particularly paranoid programmers; rather it means
that data is concealed within a class so that it cannot be accessed mistakenly by functions
outside the class.
The primary mechanism for hiding data is to put it in a class and make it private.
Private data or functions can only be accessed from within the class. Public data or functions, on
the other hand, are accessible from outside the class.
Private And Public(cont)
Class Data
The smalljob class contains one data item: somedata, which is of type int.
The data items within a class are called data members (or sometimes member
data).
There can be any number of data members in a class, just as there can be any
number of data items in a structure.
The data member somedata follows the keyword private, so it can be accessed
from within the class, but not from outside.
Member Functions
Member functions are functions that are included within a class.
(In some object-oriented languages, such as Smalltalk, member functions are called
methods; some writers use this term in C++ as well).
The function bodies of these functions have been written on the same line as the
braces that
delimit them.
You could also use the more traditional format for these function definitions:
void setdata(int d)
{
somedata = d;
}
void showdata()
{
cout << “\n Data is “ << somedata;
}
However, when member functions are small, it is common to compress
their definitions this way to save space.
Because setdata() and showdata() follow the keyword public, they can
be accessed from outside the class.
We’ll see how this is done in a moment.
Figure 6.3 shows the syntax of a class definition.
Functions Are Public, Data Is Private
Usually the data within a class is private and the functions are public. This is a
The data is hidden so it will be safe from accidental manipulation, while the
functions that operate on the data are public so they can be accessed from outside
the class.
However, there is no rule that says data must be private and functions public; in
sometime you may find you’ll need to use private functions and public data.
Simple Program For Book
#include <iostream>
class Book
public:
string title;
string author;
int price;
int pages;
};
int main()
{
Book book1;
book1.title = "C++ LEVEL 1";
book1.author = "ALI";
book1.price = 160;
book1.pages = 17;
cout << "Book1" << endl;
cout << "Book1 title " << book1.title <<"\n" << endl;
cout << "Book1 author " << book1.author <<"\n"<< endl;
cout << "Book1 price " << book1.price <<"\n"<< endl;
cout << "Book1 pages " << book1.pages<<"\n" << endl;
return 0;
}
The O/P Is
Book1
Book1 title C++ LEVEL 1
Book1 pages 17
Calling Data And Member Function
Member Function
The function should perform the same task irrespective of the place of
definition.
Outside definition:
•An important difference between a member function and normal
function is that a member function include a membership ‘identity
label’ in the header i.e class_name::
•This ‘label’ tells the compiler which class the function belongs to.
Syntax
#include <iostream>
}
O/P
50
The member function have special characteristics that are often used in
the program development. Characteristics are:
#include<iostream>
class Book
public:
string name;
string author;
int price;
// this is call member function and it print all the output
void print()
{
cout << "---------------------"<<"\t"<<endl;
cout << "Name" << "\t" << name << endl;
cout << "Author" << "\t" << author << endl;
cout << " Price" << "\t" << price << endl;
}
};
int main()
{
Book B;
}
};
int main()
{
Car C,C1;
C.name = "KIAA";
C.price = 1200;
C.model = 2023;
C1.name = "TOYOTA";
C1.price = 10000;
C1.model = 2018;
C.print();
C1.print();
}
Making outside function inline:
when we define member function outside the class definition and still
make it inline by just using the qualifier inline in the header line of
function definition
Write A Program To Create 2 Object And Class
Name as you like (By Using Member Function)
With 3 Variables()
#include<iostream>
using namespace std;
class Car
{
public:
string name;
int price;
int model;
// USING MEMEBER FUNCTION
void print()
{
cout << "----------------" << endl;
cout << " Name = " << name << endl;
cout << " Price = " << price << endl;
cout << " Model = " << model << endl;
}
};
int main()
{
Car C,C1;
C.name = "KIAA";
C.price = 1200;
C.model = 2023;
C1.name = "TOYOTA";
C1.price = 10000;
C1.model = 2018;
C.print();
C1.print();
}
O/P
Name = KIAA
Price = 1200
Model = 2023
----------------
Name = TOYOTA
Price = 10000
Model = 2018
Calling Data
Similar to accessing a data member in the class, we can also
access the public using the dot operator(.).
Example :
int main()
{
class_name obj_name;
Obj_name.member_function_name;
}
Simple program of data calling
#include<iostream>
class company
public :
int id;
string name;
float salary;
// this is function using to receive values
company c;
c.display();
return 0;
}
O/P
1500
ali
666.8
Write a program for calling data class name is Book
#include<iostream>
using namespace std;
class Book
{
public:
string name;
int no;
void save(string n, int i)
{
name = n;
no = i;
}
void display()
{
cout << "\n" << name << "\n" << no << endl;
}
};
int main()
{
Book B;
B.save("JAVA", 20);
B.display();
return 0;
}
O/P
JAVA
20
Scope Resolution Operators
The Scope Resolution Operators is used to reference the global
variable or member function that is out of scope.
For namespace
It always points to the instance of the class making the function call.
int width;
int lenght;
int height;
int print();
};
int Rect::print()
{
return width*lenght*height;
}
int main()
{
Rect op;
op.width = 2;
op.lenght = 3;
op.height = 5;
cout<<op.print()<<endl;
return 0;
}
a)Write a class program using member function and
the name of class is patient with 4 object and no
less than with 3 variables?