Object and class

PART A

Q1. Distinguish between the following:

a)OBJECT AND CLASS:

Ans:

(1) An object has identity,state,bahaviour, and property while class describes a group of similar properties, common behaviour, common relationships to other objects and common semantics.

(2) For a single class, there may be any number of objects instantiated at any one time.

(3) objects may be created and destroyed at run time whereas once a class defined,it exists all time a program is running.

b) ABSTRACTION AND INCAPSULATION:

Ans:(1)

Abstraction refers to act of representing essential features without inclding background details while Wrapping up of data and functions into a single unit is known as Encapsulation.

Q2. Explain

(a)MESSAGE PASSING:

Ans:A message passing for an oject is a request for execution of a procedure,and therefore will invoke a function in the receiving object that generates the desired result.Message passing involves specifying the name of the object,the name of the function and the the information to be sent.

(b)DYNAMIC BINDING:

Ans:It is also called late binding.In it, the events occur at run-time.it is called dynamic binding because the selection of the appropriate function is done dynamically at run time.Dynamic binding is one of the powerful features of c++.This requires the use of pointer to objects.

Object pointer are useful in creating objects at run-time.The main advantage of dynamic binding is flexibility at the run-time.

(c)OVERLOADING:

Ans:Overloading means that more than one function or method can be defined with the same name but differene parameter lists,by type/or number of parameters.For each different set of parameters, a different definition of named function is implemented.

(d)OBJECT- BASED LANGUAGES:

Ans:It is the style of programming that primarily supports encapsulation and object identity.major features are required for object based programming are-

(1) Data encapsulation

(2)Data hiding and access mechanisms

(3)Automatic initialization and clear-up of objects

(4)Operator overloading

(e)OBJECT- ORIENTED LANGUAGES:

Ans:

It incorporates all of object-based programming features along with two additional features, namely, inheritance and dynamic binding.Object-oriented language can therefore be characterized by the following statements-

object-based features+inheritance+dynamic binding

Q3.What is meant by interface of a class and implementation of a class?

ANS:

INTERFACE OF A CLASS:

C++ is very efficient about not wasting time on things that are not used.If our class has no data members or base classes and no user defined constructor, then we've never seen a compiler that generates any code on construction or destruction.
Hence a C++ interface is just a class that has: No non-static data members. Pure virtual methods for the interface routines.
No base classes that aren't themselves interfaces.

IMPLEMENTATION OF A CLASS:

The C++ programming language allows programmers to separate program-specific datatypes through the use of classes. Instances of these datatypes are known as objects and can contain member variables, constants, member functions and overloaded operators defined by the programmer. Syntactically, classes are extensions of the C structs which cannot contain functions or overloaded operators.

Q4. Differentiate between classes and structures with appropriate examples?

Ans:

In C++, a structure is a class defined with the struct keyword. Its members and base classes are public by default. A class defined with the class keyword has private members and base classes by default. This is the only difference between structs and classes in C++.

example of structure

struct student

{

int roll;

char grade;

float per;

};

void main()

{

struct student s;

s.roll=10;

s.grade='A';

s.per=75;

cout<<"Roll="<<s.roll<<"grade="s.grade<<"per="<<s.per<<endl;

getch();

}

example of class:

class student

{

private:int roll;

char grade;

float per;

public:

void get();

void show();

};

void student::get()

{

cout<<"enter roll,grade and per";

cin>>roll>>grade>>per;

}

void student::show()

cout<<"Roll="<<roll<<"grade="grade<<"per="<<per<<endl;

void main()

student s;

s.get();

s.show();

}

PART B

Q5. Create a class to represent a bank account. It should include the following:

Data Members:

Name of account holder

Account number

Type of account

Balance account

Member Functions:

To initialize the data members with appropriate data

To deposit an account

To withdraw an amount after checking the balance

To display details of account holder

ANS

#include<iostream.h>

#include<conio.h>

class account

{ char nam[30];

char tpe[10];

float bal;

public

int accno;

void init()

{

cout<<"\n enter an account number :";

cin>>accno;

cout<<"\n enter name of accountee :";

gets(nam);

cout<<"\n enter balance :";

cin>>bal;

cout<<"\n enter type of account S for saving and C for current :";

gets(tpe);

}

void disp()

{

void deposit(float x)

{

bal=bal+x;

}

void withraw(float y)

{

if(bal>y)

bal=bal-y;

else

cout<<"amount in account is less than to withdraw";

}

}

void main()

{

clrscr();

account a[10];

for(int i=0;i<10;i++)

{

a[i].init();

}

int acno;

cout<<"\n enter an account number ";

cin>>acno;

for(int j=0;j<10;j++)

{

if(acno==a[j].accno)

{

cout<<"\n select the menu";

cout<<"\n to withraw :w ";

cout<<"\n to deposit :d";

cout<<"\n to disply acoount info :i";

char ch;

cin>>ch;

if(ch=='w')

{

cout<<"\n enter an amount to withdraw :";

float w;

cin>>w;

a[j].withdraw(w);

}

else

if(ch=='d')

{

cout<<"\n enter an amount to deposit :";

float d;

cin>>d;

a[j].deposit(d);

}

else

if (ch=='i')

a[j].disp();

else

cout<<"\n entered choice is wrong :";

}

}

if(j==10)

cout<<"\n no such accountee exists ";

}

getch();

}

6:Define instantiation of a class. Is it possible to assign data from one object of a class to another object of same class? Explain with examples

Ans:Just as writing a class declaration was an extension of concepts we've already covered, the same is true of the syntax needed to instantiate an object. Instantiating an object is what allows us to actually use objects in our program. we can write hundreds and hundreds of class declarations, but none of that code will be used until you create an instance of an object. A class declaration is merely a template for what an object should look like. When you instantiate an object, C++ follows the class declaration as if it were a blueprint for how to create an instance of that object.

Yes it is possible to assign data from one object of a class to another object of same class when the data of the same class is "public" defined

Q7. Give the classification of various access specifiers. How are they different from each other?

Ans:The keywords private, public, and protected are known as access specifier or visibility labels.

A member of a class can be private,protected, or public-

(1)if it is private,its can be used only by member functions and friends of the class in which it is declared.

(2)if it is protected, its name can be only by declared and by member functions and friends within its class and any class derived immediately from it.

(3)if it is public,its name can be used by any function.

DIFFERENCE BETWEEN THEM:

Private data or functions can only be accesed from within the class.Public data or the other hand,are accessible from outside the class.usually the data within a class is private so it will be safe from accidental manipulation,while functions that operate on data are public,so they can be accessed from outside the class.However, in some circumstances,we will need to use private functions and public data.

Please be aware that the free essay that you were just reading was not written by us. This essay, and all of the others available to view on the website, were provided to us by students in exchange for services that we offer. This relationship helps our students to get an even better deal while also contributing to the biggest free essay resource in the UK!