0% found this document useful (0 votes)
18 views47 pages

Unit I & Ii

The document provides an overview of classes and objects in C++, detailing the structure of a class, access specifiers (public, private, protected), and the concept of constructors and destructors. It explains how to declare and define classes, member functions, and the different types of constructors, including default, parameterized, and copy constructors. Additionally, it covers advanced topics like friend functions, local and nested classes, and abstract classes.

Uploaded by

Best Songs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views47 pages

Unit I & Ii

The document provides an overview of classes and objects in C++, detailing the structure of a class, access specifiers (public, private, protected), and the concept of constructors and destructors. It explains how to declare and define classes, member functions, and the different types of constructors, including default, parameterized, and copy constructors. Additionally, it covers advanced topics like friend functions, local and nested classes, and abstract classes.

Uploaded by

Best Songs
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 47

UNIT-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.

. The most common form of copy constructor is shown here:


classname (const classname &obj)
{
// body of constructor
}
contd…
Contd…
Destructor
C++ Destructor Basics
Destructors are special member functions of the
class required to free the memory of the object
whenever it goes out of scope.
Destructors are parameterless functions.
Name of the Destructor should be exactly same
as that of name of the class. But preceded by ‘~’
(tilde).
Destructors does not have any return type. Not
even void.
The Destructor of class is automatically called
when object goes out of scope.
C++ Destructor programs
Dymanic constructor
Dynamic constructor is used to allocate the
memory to the objects at the run
time.Memory is allocated at run time with the
help of 'new' operator.

By using this constructor, we can dynamically


initialize the objects.
Constructor Overl oading in C++

n C++, We can have more than one constructor in


a class with same name, as long as each has a
different list of arguments.This concept is known
as Constructor Overloading and is quite similar to
function overloading.
Overloaded constructors essentially have the
same name (name of the class) and different
number of arguments.
A constructor is called depending upon the
number and type of arguments passed.
While creating the object, arguments must be
passed to let compiler know, which constructor
needs to be called.
example
Friend Function
A friend function of a class is defined outside
that class' scope but it has the right to access
all private and protected members of the
class. Even though the prototypes for friend
functions appear in the class definition,
friends are not member functions.
To declare a function as a friend of a class,
precede the function prototype in the class
definition with keyword friend
Bullets…..
Declaration of friend function in C++
Demo of friend function
example
Local Classes in C++
A class declared inside a function becomes
local to that function and is called Local Class
in C++.
A local class name can only be used locally
i.e., inside the function and not outside it.
The methods of a local class must be defined
inside it only.
 void fun()
{
 class Test // local to fun
 {
 public:

 // Fine as the method is defined inside the local class
 void method()
{
 cout << "Local Class method() called";
 }
 };

 Test t;
 t.method();
}

 int main()
{
 fun();
Nested class
A nested class is a class that is declared in
another class. The nested class is also a
member variable of the enclosing class and
has the same access rights as the other
members. However, the member functions of
the enclosing class have no special access to
the members of a nested class.
Example
Abstract Class
An abstract class is a class that is designed to be
specifically used as a base class. An abstract class
contains at least one pure virtual function. You
declare a pure virtual function by using a pure
specifier ( = 0 ) in the declaration of a virtual
member function in the class declaration
Abstract classes and methods are when the parent
class has a named method, but need its child
class(es) to fill out the tasks. An abstract class is a
class that contains at least one abstract
method. An abstract method is a method that is
declared, but not implemented in the code.

You might also like