Class n Object in C++

0
175
views
Class object c++

Lets Start learning Class n Object in C++

CLASS:-

A class is a set of operations (public)and set of data(private)

SYNTAX:- class classname

{

Accessspecifier1:

P     Datamembers1;

Member functions1;

Accessspecifier2;

Datamembers2;

Member functions 2;

Member functions;

}

Data is defined as variables and those are called as “DATA MEMBERS

And functions are defined in the body of the class are called as “MEMBER FUNCTION“.

Access Specifiers in c++

Access specifiers are defined the visibility of members.

There are 3 types of access specifiers:

1.PUBLIC

2.PRIVATE

3.PROTECTED

* The public data members can be accessed within the class as well as outside the class.

  • The private data members can be accessed only within the class.

✓The default accessed specifier in the body of the class is private .

So,if any access specifier is not provided then the following members are considered as private.

So,if any access specifier is not provided then the following members as considered as private.

W.A.P on accessing the class members:

Program

#include<iostream.h>

#include<conio.h>

Class marks

{

Int regd no;

Int totmarks;

Public:

Void setmarks(int r,int t)

{

Regdno=r;

Totmarks=t;

}

Void dispmarks(.  )

{

Cout<<“registered no:”<<regdno<<endl;

Cout<<“total marks”<<totmarks<<endl;

}

};

Void main( )

{

Marks rm;

Clrscr( );

rm.setmarks(4324,300);

rm.dispmarks( );

Marks m1,m2;

m1.set marks(12341,534)

m2.set marks(12342,332);

Cout<<“marks of 2 students:”<<endl;

m1.dispmarks( );

m2.dispmarks( );

getch( );

}

Output:

Register no:4324

Total marks:300

Marks of 2 students:

Register no:12341

Total marks:534

Register no:12342

Total marks:332

OBJECT

an object is an instance of a class in genral terms,object is also considered as variable but releated

to specific type class i.e.,object is a variable whose data type is class.

->class can be defined as word defined datatype

SYNTAX-1:

classname object1,object2…….objectn;

SYNTAX-2:

class classname obj1,obj2,…..objn;

SYNTAX-3:

class classname

{

………..

………..

}obj1,obj2,…..objn;

PROGRAM:

#include<iostream.h>

#include<conio.h>

class item

{

int num;

float cost;

public:

voidgetdata(int a,float b);

voidputdata(void)

{

cout<<“number”<<number<<“\n”;

cout<<“cost”<<cost<<“\n”;

}

};

void item::getdata(int a ,int b)

{

num=a;

cost=b;

}

void main()

{

item x;

clrscr();

cout<<“\n objects x”<<“\n”;

x.getdata(100,299.95);

x.putdata();

getch();

}

Ouput:-

number 100

Cost 299.95

REFERENCE VARIABLE

When a variable is declared as reference,it become an alternative name for an existing variable A variable can be declared as reference by putting ‘&’in the declaration.

References vs pointer

Both references and pointers can be used to change local variables of one functions inside another functions.both of them can be used to save copying of big objects when passed as argument to functions or returned from functions, to get efficiency gain.Despite above similarities,there are following differences between references and pointers

A pointer can be declared as void but a reference can be void

Reference variable :-Aces as both value and pointers variable.

Value variable:-holds some numeric value.

Syntax for variable:-datatype &reterence =value variable;

Eg.int&sec=first;

:-Now it is change the data to varaiable first,it effects to variable sec and vice-versa.

Eg.char&=6;//character reference variable;

Double&d=g;//double reference variable;

Int&y=a[30];//reference variable for array;

i.e int x=300; is invalid

Program

#include<iostream.h>

Void main()

{

Int a=20;

Int&x=a;

Clrscr();

Cout<<a<<”—“<<x<<endl;

a=30;

cout<<a<<”—“<<  x<<endl;

x=200;

cout <<a<<”—“<<x<<endl;

getch();

}

Output:-

X=30 30

X=200 200

Constructor

An object requires memory and to be initialised with some values .

A constructor is a special function used for initialising data members of an object.

Constructor is a member function whose name is same as the classname .

It is used to create the objects of the class type.

Constructor function initialises data members and also used for allocating storage from the heap memory with the help of “new operator”.

Constructors can be overloaded and take arguments.

Constructors don’t have return type and those can’t use return expression statements.

Destructor

A destructor is a member function whose name is same as the classname preceded by the “~” (tild character).

The purpose of destructor is destroying the objects of the class type

It is useful to deallocate the memory allocated by constructor.

Frequently it uses delete operator to deallocate object memory.

Destructors also don’t have any return type expression statements.

Note:-

Constructor and destructor must be declared in the public section otherwise they can’t be invoked.

A zero argument constructor is called as default constructor.

 program to illustrate the use of constructor and destructor

#include<iostream.h>#include<conio.h>class stu{private: char name[20],add[20];int roll,zip;public: stu ( );//Constructor~stu( );//Destructorvoid read( );void disp( );};stu :: stu( ){cout<<”This is Student Details”<<endl;}void stu :: read( ){cout<<”Enter the student Name”;cin>>name;cout<<”Enter the student roll no “;cin>>roll;cout<<”Enter the student address”;cin>>add;cout<<”Enter the Zipcode”;cin>>zip;}void stu :: disp( ){cout<<”Student Name :”<<name<<endl;cout<<”Roll no   is       :”<<roll<<endl;cout<<”Address is       :”<<add<<endl;cout<<”Zipcode is       :”<<zip;}stu : : ~stu( ){cout<<”Student Detail is Closed”;} void main( ){stu s;clrscr( );s.read ( );s.disp ( );getch( );}

Output:

Enter the student Name
James
Enter the student roll no
01
Enter the student address
Newyork
Enter the Zipcode
919108

Student Name : James
Roll no is : 01
Address is : Newyork
Zipcode is :919108

CONSTRUCTOROVERLOADING:

Ithesyntaxesareusedinthesameclassthenthoseconstructorsarecalledasconstructoroverloading.

COPYCONSTRUCTOR:

Aconstructorwhichishavingareferencetoaninstanceofitsownclassasanargumentiscalledcopyconstructor.

SYNTAX:

classclassname

{

datamembers;

public:

classname(classname&object)

{

}

};(or)

classnameobjectname(objectname1);(or)

classnameobjectname2=objectname1;

PROGRAM on constructor overloading

#include<iostream.h>

#include<conio.h>Classcomplex

{

Floatx,y;Public:Complex()

Complex(floata){x=y=a;}Complex(floatreal,floatimag)

{X=real;y=imag;}

Friendcomplexsum(complex,complex)Friendvoidshow(complex);

};

Complexsum(complexC1,complexC2)

{

ComplexC3;C3.x=c1.x+c2.x;C3.y=c1.y+C2.y;Return(C3)}

Voidshow(complexc)

{

Cout<<c.x<<”+j”<<c.y<<“\n”;

}

voidmain(){

complexA(2.7,3.5);complexB(1.6);complexC;

C=sum(a,b);

Cout<<“A=“;show(A);Cout<<“B=“;show(B);Cout<<“C=“;show(C);

getch();

}

Output-. 

A=2.7+j3.5

B=1.6+j1.6

C=4.3+j5.1

PROGRAM on copy constructor

#include<iostream.h>

#include<conio.h>

classtc

{

intd1,d2;

public:tc()

{

cout<<“defaultconstructorinvoked”;

d1=111;d2=222;

tc(tc&t)

{

cout<<“copyconstructorinvoked”;

d1=t.d1;d2=t.d2;

}

};

voidmain()

{clrscr();tct1;

tct2;

tct3(t1);tct4=t3;getch();

}

     Output:-

Default constructor invoked

Default constructor invoked

Copy constructor invoked

Copy constructor invoked

For more details visit our HomePage

Class n Object in C++