Unit I & Ii
Unit I & Ii
C++
CLASSES AND OBJECT
A class in C++ is a user defined type or data
structure declared with keyword class that
has data and functions (also called methods)
as its members whose access is governed by
the three access specifiers private,
protected or public (by default access to
members of a class is private).
Objects are instances of class, which holds
the data variables declared in class and the
member functions work on these class
objects.
Declararion of a class
It involves four attributes:-
Data Member :- Describes the characteristics
of a class. There may be zero or more members
in the class
Member Function:- Are set of operation that
may be applied to the object of that class.
Program Access Level:- controls access to
members within the program.
Class Tagname:- Serves as a type specifier for
the class using which object of this class type
can be created.
The class Definition
Class class_name
{
Private:
Variable declaration;
Function declaration;
Protected:
Variable declaration;
Function declaration;
Public:
Variable declaration;
Function declaration
};
Access Specifiers in c++
Access specifiers in C++ class defines the
access control rules. C++ has 3 new
keywords introduced, namely,
public
private
protected
Public
Public, means all the class members declared under public
will be available to everyone. The data members and
member functions declared public can be accessed by
other classes too. Hence there are chances that they might
change them. So the key members must not be declared
public.
class PublicAccess
{
public: // public access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
}
Protected
Protected, is similar to private, it makes class member
inaccessible outside the class. But they can be
accessed by any subclass of that class. (If class A is
inherited by class B, then class B is subclass of class
A. We will learn this later.)
class ProtectedAccess
{
protected: // protected access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
}
Private
Private keyword, means that no one can access
the class members declared private outside that
class. If someone tries to access the private
member, they will get a compile time error. By
default class variables and member functions are
private.
class PrivateAccess
{ private: // private access specifier
int x; // Data Member Declaration
void display(); // Member Function decaration
}
Summary of Access Specifiers
public - members are accessible from outside
the class
private - members cannot be accessed (or
viewed) from outside the class
protected - members cannot be accessed
from outside the class, however, they can be
accessed in inherited classes.
example
class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};
int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Example of class declaration(inside the class definition)
class MyClass
{
public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
int main() {
MyClass myObj; // Create an object of MyClass
myObj.myNum = 15; // Access attributes and set values
myObj.myString = "Some text";
cout << myObj.myNum << "\n"; // Print attribute values
cout << myObj.myString;
return 0;
}
Member Function Definition
It can be done by two methods
Inside the class definition
Outside the class definition
Inside The Class definition
When the member function is defined inside a
class the function definition is just similar to
the function definition. In this type of
definition we donot need to put membership
label classname
Example of class declaration(outside the class definition)
class programming
{
public:
void output(); //function declaration
}; // function definition outside the class
void programming::output()
{
cout << "Function defined outside the class.\n"; }
int main()
{
programming x;
x.output();
return 0;
}
Assignments(classes & objects)
Assignment 1
WAP that will ask for a name and marks in 5
subjects from the students and display all the
information.
Assignment 2
WAP that will ask for a name and marks of five
subject of 5 students.
Assignment -3
Define a class book with the followingspecification:-
Book no integer type
Book_title 20 character
Price float (price per copy)
Total_cost() A function to calculate the total
cost where N is passed as a
parameter to the function.
Public member of the class book are
Input() Function to read Book_no,
Book_title, price
Purchase() function to ask the user to input the
no. of copies to be purchased. It calls
total_cost() function and print the
cost.
Constructor
A constructor is a member function of a class
thai is automatically called when an objects
created of that class. It has the same name as
that of class and its primary job is to
initialize the object to a legal initial value for
the class.
A member Function with the same name as
its class is called constructor and it is used
to initialize the object at that class type with a
legal initial value.
Contd…
Constructor is automatically called when
object(instance of class) create.It is special
member function of the class.Which
constructor has arguments thats called
Parameterized Constructor.
It has same name of class.
It must be a public member.
No Return Values.
Default constructors are called when
constructors are not defined for the classes.
Demo
Class student
Class student
{
{
Private:
Private:
int rollno;
Float marks; int rollno;
Public: Float marks;
Void initialize() Public:
{ Void student()
Rollno=1; {
Marks=100; cout<<“enter roll no and
cout<<“enter roll no and marks”<<rollno<<marks;
marks”<<rollno<<marks; Cin>>rollno>>marks;
}
}
Void display()
Void display()
{
Cout<<marks;
{
Cout<<rollno; Cout<<marks;
} Cout<<rollno;
}; }
};
Void main() Void main()
{ {
Student c; Student c;
c.getdata(); c.display();
c.dispay(); Getch();
Getch(); }
}
ASSIGNMENT
Wap to calculate the sum of two nos . The nos
should be initialized by using the contructor.
Types Of Constructor
There are three types of constructor
1. Default constructor
2. Parameterised contructor
3. Copy constructor
Default constructor
The Default constructor is a special member
function with no argument.
OR
A default constructor is a constructor that
either has no parameters, or if it has
parameters, all the parameters have default
values.
If no user-defined constructor exists for a
class A and one is needed, the compiler
implicitly declares a default parameterless
constructor
Parameterised constructor
It may be necessary to initialize the various
data
elements of different objects with different
values when they are created.This is achieved
by passing arguments to the constructor
function when the objects are created.The
constructors that can take arguments are
called parameterized constructors.
Types of passing argument in constructor
Implicit call:-The implicit call is implemented
as
student heena(120, 60)
This method is called shorthand method and is
very often use at the same time. It is handy
too(Easy to implement)
Explicit call:-
student mohan=student(120,60)
class myclass
{
int a, b;
public:
myclass(int i, int j)
{
a=i;
b=j;
}
void show()
{
cout<< a << " " << b;
}
};
void main()
{
clrscr();
myclass ob(3, 5);
ob.show();
getch();
}
Copy constructor
The copy constructor is a constructor which creates an
object by initializing it with an object of the same class,
which has been created previously. The copy constructor
is used to:
Initialize one object from another of the same type.
All member values of one object can be assigned to the
other object using copy constructor.
For copying the object values, both objects must belong
to same class.