You are on page 1of 6

• Object oriented programming

Programming consists of designing a set of objects that model the


problem at hand. (Programs are made up of objects )
• Data is hidden from external functions
• Objects can communicate with each other through methods

• Classes
– Model objects that have attributes (data
members) and behaviors (member functions)
– Defined using keyword class
– Have a body delineated with braces ({ and })
Example:

Declaring a Class
To declare a class, use the class keyword followed by an opening brace, and then list the data
members and methods of that class. End the declaration with a closing brace and a semicolon(for
c++). Here's the declaration of a class called Cat:
class Cat
{
public:
unsigned int itsAge;
unsigned int itsWeight;
Meow();
};

Defining an Object
You define an object of your new type just as you define an integer variable:

unsigned int GrossWeight; // define an unsigned integer


Cat Frisky; // define a Cat

This code defines a variable called Gross Weight whose type is an unsigned integer. It also
defines Frisky, which is an object whose class (or type) is Cat.

Accessing Class Members


Therefore, to assign 50 to Frisky's Weight member variable, you would write
Frisky.itsWeight = 50;
In the same way, to call the Meow() function, you would write
Frisky.Meow();
When you use a class method, you call the method. In this example, you are calling Meow() on
Frisky.

Assign to Objects, Not to Classes


you can define an integer variable and assign 5 to that variable. For example,
int x; // define x to be an int
x = 5; // set x's value to 5

This is a shorthand way of saying, "Assign 5 to the variable x, which is of type int." In the same
way, you wouldn't write
Cat.itsAge=5; // wrong

The compiler would flag this as an error, because you can't assign 5 to the age part of a Cat.
Rather, you must define a Cat object and assign 5 to that object. For example,
Cat Frisky; // just like int x;
Frisky.itsAge = 5; // just like x = 5;

Private Versus Public


Other keywords are used in the declaration of a class. Two of the most important are public and
private.

All members of a class, data and methods, are private by default. Private members can be
accessed only within methods of the class itself. Public members can be accessed through any
object of the class. This distinction is both important and confusing. To make it a bit clearer,
consider an example from earlier in this chapter:

class Cat
{
unsigned int itsAge;
unsigned int itsWeight;
Meow();
};

In this declaration, itsAge, itsWeight, and Meow() are all private, because all members of a class
are private by default. This means that unless you specify otherwise, they are private.

However, if you write

Cat Boots;
Boots.itsAge=5; // error! can't access private data!

The compiler flags this as an error.

The way to use Cat so that you can access the data members is:

class Cat
{
public:
unsigned int itsAge;
unsigned int itsWeight;
Meow();
};
Now itsAge, itsWeight, and Meow() are all public. Boots.itsAge=5 compiles without problems.

Accessor functions enable you to separate the details of how the data is stored from how it is
used. This enables you to change how the data is stored without having to rewrite functions that
use the data.

Constructor:

 When the class “student” is created, we might want to perform some special routine
o to initialise the values of some data attributes, perhaps
o or to cause the newly created object to register its existence with something else -
perhaps some kind of “manager”.
 This functionality can be embedded in a special member function called the constructor.
 Every class has a default constructor which is automatically called when an object of that class is
created
o to allocate memory.
o We can extend this default constructor.
 Our constructor is identified by having the same member function name as the class name.
 The class constructor is automatically invoked when the object is created.
 Constructors have a pre-defined return type, i.e. a pointer to the object itself.

Examples:

Class student{

Private:

Char name[20];

int age;

Public:

Void getd()

{cout<<“enter info…”;

Cin>>name>>age;

Void disp();

};

Void student:: disp()

{Cout<<name<<age<<endl;

Void main()

{ Student s1;

S1.getd();

S1.disp();

// if it is for n students

Student s1[20;

Int I,n=3;

For( i=0;i<n;i++)
S1[i].getd();

For( i=0;i<n;i++)

S1[i].disp();

Examples: (structure with array )

#include<iostream.h>

#include<conio.h>

struct student{

char name[20];

int age;

};

void disp(struct student s1)

if(s1.age>20)

cout<<s1.name<<"\t"<<s1.age<<endl;

void main()

{ clrscr();

student st[20];

int n,i;
cout<<"how many?";

cin>>n;

cout<<"enter the info:";

for(i=0;i<n;i++)

cin>>st[i].name;

cin>>st[i].age;

cout<<"result\n";

for(i=0;i<n;i++)

//cout<<st[i].name<<"\t";

disp(st[i]);

getch();

You might also like