Chapter 2
Chapter 2
struct Person
{ char name[10]; Members or Fields of structure
int age;
float salary;
}
How to create a structure?
‘struct’ keyword is used to create a structure.
How to declare structure variables?
A structure variable can either be declared with structure declaration or as a separate declaration like
basic types.
// A variable declaration with structure declaration. // A variable declaration like basic data types
struct Point
{ struct Point
int x, y; { int x, y;
} p1; // The variable p1 is declared with 'Point’ };
int main()
{
struct Point p1; // The variable p1 is declared like a
normal variable
}
How to access structure elements?
Structure members are accessed using dot (.) operator.
#include<iostream>
struct Point
{
int x, y;
};
int main()
{
struct Point p1 = {0, 1};
return 0;
}
Limitations of C Structures
The C structure does not allow the struct data type to be treated like built-in data types
We cannot use operators like +,- etc. on Structure variables. For example, consider the following code:
struct number
{
float x;
};
int main()
{
struct number n1,n2,n3;
n1.x=4;
n2.x=3;
n3=n1+n2;
return 0;
}
/*Output:
*/
Limitation…..
Extension to Structure
• C++ define all the features of structure as defined in C.
• C++ add the concept of OOP.
• In C++, a structure can have both the functions and variables as members.
All these extensions are incorporated in another user defined type known as
CLASS.
* By default, the member of class are private, while, by default , the members of
a structures are public.
Class & Object
• A class is a way to bind the data and its associated functions together. It allows the data and functions to be
hidden, if necessary, from external use. A class declaration is similar syntactically to a structure.
Declaration of Class:
Class class_name
{
private:
variable declaration;
function declaration;
public:
Variable declaration;
function declaration;
}
Points to remember!
❑The variables declared inside the class definition are known as data members and the
functions declared inside a class are known as member functions.
❑Wrapping of data and function and function into a single unit (i.e. class) is known as data
encapsulation.
❑By default the data members and member function of a class are private.
❑Private data members can be accessed by the functions that are wrapped inside the class.
General steps to write a C++ program using class and object:
• Header files
• Class definition
• Member function definition
• void main function
Example:
# WAP to find the SUM of two integers void Add :: calculate()
using class and object: {
#include<iostream> z=x+y;
using namespace std }
class Add void Add :: display()
{ {
int x, y, z; cout<<z;
public: }
void getdata() void main()
{ {
cout<<”Enter two numbers”; Add a;
cin>>x>>y; a.getdata();
} a.calculate();
void calculate(void); a.display();
void display(void); }
}; Output:
Enter two numbers 5 6
11
A member function can be defined:
(i) Inside the class definition
(ii) Outside side the class definition using scope resolution
operator (::)
Memory allocation for Objects
• Memory space for objects is allocated
when they are declared not when
class is specified.
• Member functions are created and
placed in the memory space only once
when they are defined.
• All objects belong to the class use the
same member function
• Space for member variables is
allocated separately for each object.
Static Data member
To remove the array that we have just created the statement would look like this −
delete [ ] pvalue; //delete array pointed by the pvalue.
Following the similar generic syntax of new operator, you can allocate for a multi-dimensional array as follows −
double **pvalue = NULL;
pvalue = new double[3][4]; // allocate memory for a 3x4 array
delete [ ] pvalue; // delete array pointed to pvalue.
Dynamic memory Allocation for Objects
• Objects are no different from simple data types. For example, consider the following code where we are
going to use an array of objects to clarify the concept −
Class Add {
int a, b;
public:
void display();
};
Int main()
{ Add *obj = new Add[10];
Delete [] obj; //delete array
Pointer to member
• It is used to assign the address of member of a class and assign to the pointer.
Definition:
Class A
{ int m;
Public:
Void show();
};
Int A ::* ip = &A :: m; //definition of pointer to member.
Here, ip pointer act like class member and it must be invoked by class object.
A::* (“pointer to member of A class”)
&A ::m (“address of the m member of A class”)
Invoke:
A a; A *ap = &a; //pointer to object a
Cout<<a.*ip; cout << ap -> *ip;
Cout<<a.m; Both will display the same output. Cout<< ap -> m; display m
• Dereferencing operator ->* used to access a member when we use pointers to both the
object and the member.
• Dereferencing operator * is used when object itself is used with the member pointer
Tilde ~classname() { … }