You are on page 1of 23

EC-6301 OOPS & DS

Structure
Collection of variables under single name.
Variables may be any type int,float,char..etc
Declared by using keyword called struct followed by
struct name also called a tag.
Syntax:
Struct tagname
{
variable declaration;
};
Example
#include<iostream.h>
struct emp
{
int num;
char name[10];
float sal;
};
Void main()
{
emp e;
cout<<“Enter Number,Name& Salary of an Employee”
cin>>e.num>>e.name>>e.sal;
cout<<“\nNumber”<<e.num<<“\nName”<<e.name<<“\nSalary”<<e.sal
}
Difference
Structure Class

Default Members of structure are Default members of class are Private

public

Cannot be Inherited Can be Inherited

Don’t Require Constructors Classes require constructors for

initializing the objects

Contain Only Data Members Contains the data as well as Member

Function
Class Scope & Accessing Class Members
Class is a Keyword.
Default privacy specification is Private.
2Types:
- class declaration -> describe the type and scope of its
members.
-Class function definitions->it describes how the class
functions are implemented.
 Data members-> variables declared inside the class
Member functions-> functions()
Syntax
Class classname
{
private data members;
private member functions;
Access Specifier:
data members;
member functions;
Access Specifier:
data members;
member functions;
};
Access Specifier /Modifiers
Access Specifier Inside class Sub Class Outside Class

Public Y Y Y

Protected Y Y N

Private Y N N
Scope Resolution operator
Scope resolution operator(::) is used to define a

function outside a class or when we want to use a


global variable but also has a local variable with same
name.
Eg With out Class
#include <iostream>  
char c = 'a'; // global variable  
int main()
{
char c = 'b'; //local variable  
cout << "Local variable: " << c << "\n";
cout << "Global variable: " << ::c << "\n"; //using scope
resolution operator  
return 0; }
Member Functions & Classes
1. Outside the class definition &

2. Inside the class definition


Member Function Outside the Class Definition
Syntax:
Return type class name::function name(argument
declaration)
{
Function body
}
Private Member Function
Object cannot call private member function

Only public member function of the same class can

call private member function


Constructor
 Constructors are special member functions (should
be declared in public section) with the same name as
the class. They do not return values.
 The constructor is invoked whenever an object of its
associated class is created.
 A constructor is called whenever an object is defined
or dynamically allocated using the “new” operator.
 They are normally used to allocate memory for the
data members and initialize them.
 Constructor function name =class name
Characteristics
Constructor name must be the same as the name of the

class.
Should be “public”.

Should not have return type(i.e void)

Not Virtual functions.

Need not be called explicitly with ‘.’ Dot operator. It

will be called automatically when an object for the


class get created
Types of Constructor
Default Constructor

Parameterized Constructor

Copy Constructor

Dynamic Constructor
Default Constructor
It has no arguments.

Also called No argument constructor

Syntax:

Classname::classname()

Statements;

}
Parameterized Constructor
Constructor can take arguments called parameterized
constructor.
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.
Syntax:
Class name::class name(type parameter1,type parameter2,..)
{
Statements;
}

You might also like