Professional Documents
Culture Documents
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
public
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
class.
Should be “public”.
Parameterized Constructor
Copy Constructor
Dynamic Constructor
Default Constructor
It has no arguments.
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;
}