Professional Documents
Culture Documents
Structures and Classes Class 1 2
Structures and Classes Class 1 2
struct structure_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
.
.
} object_names;
THE FIRST THING WE HAVE TO KNOW
IS THAT…….
• a data structure creates a new type:
• book.title
• book.author
• …………..
• …..
NOTATIONS FOR ACCESS- THESE
ARE EQUIVALENT
• a->b
• (*a).b
• Case2
• ptr ->name
• ptr->roll
THE -> IS CALLED THE ARROW
OPERATOR
• It is formed by using the minus sign followed by a greater
than sign.
• To access members of a structure, use the dot operator.
• To access members of a structure through a pointer, use
the arrow operator.
CLASS
class class_name
{
….
Any valid
….
identifier
….
};
Class body (data member +
methods)
Figure 10-2 Class access specifiers
OBJECT
• Within the body, the keywords private: and public: specify the
access level of the members of the class.
• the default is private.
class class_name
{ private members or methods
private:
…
…
…
public: Public members or methods
…
…
…
};
• Any member that is declared before one other class specifier
automatically has private access.
CLASS METHODS DEFINITION
• area:12
POINTS
• they have private access and they can only be referred from
within other members of that same class.
CLASSES IN C++
• Protected …………?
WHY SUCH ACCESS SPECIFIERS ???
class Abc
{
int x,y ;
public:
int z;
int add (inta ,int b)
{
int c=a+b;
return c ;
}
int sub (int a,int b)
{ int c =a-b;
return c;
};
Abc O1,O2;
PUBLIC AND PRIVATE MEMBERS
• O1.sub(3,7) …..
• O2.add (6,8) ………
• sub (16,8) …………
• O1.z=6 …………….
• O2.x =9 ……………..
• O1.y =7 ………….
• O2.z =5 ……………..
CLASS EXAMPLE
int main()
{
sample K:
K.setvalue();
cout<<“mean value= “ << mean(K) <<endl;
return 0;
}
• Write example cases of two classes using a friend. The
function can be a member function for one class and a friend
for the other class.
FUNDAMENTALS OF OOP
• Class
• Object
• Encapsulation
• Abstraction
• Inheritance
• Polymorphism
•
REASONS FOR OOP
1. Simplify programming
2. Interfaces
• Information hiding:
• Implementation details hidden within classes themselves
3. Software reuse
• Class objects included as members of other classes