Вы находитесь на странице: 1из 19

Cse 202 object oriented

programming
Ans 1

Definition (Class): A class is the implementation of an abstract


data type (ADT). It defines attributes and methods which
implement the data structure and operations of the ADT,
respectively. Instances of classes are called objects.
Consequently, classes define properties and behaviour of sets of
objects.

Definition (Object): An object is an instance of a class. It can be


uniquely identified by its name and it defines a state which is
represented by the values of its attributes at a particular time.

The state of the object changes according to the methods which


are applied to it. We refer to these possible sequence of state
changes as the behaviour of the object:

Ans 2

Object Oriented Programming Paradigm:

Oops is a better way of solving problems in computers compared


to the procedural language programming such as in C. oops is
designed around the data being operated upon as opposed to
the operations, these operations are designed to fit data.

A type of programming in which programmers define not only


the data type of a data structure, but also the types of
operations that can be applied to the data structure. In this way,
the data structure becomes an object that includes both data
and functions. In addition, programmers can create
relationships between one object and another. For example,
objects can inherit characteristics from other objects.

One of the principal advantages of object-oriented programming


techniques over procedural programming techniques is that
they enable programmers to create modules that do not need to
be changed when a new type of object is added. A programmer
can simply create a new object that inherits many of its features
from existing objects. This makes object-oriented programs
easier to modify.

To perform object-oriented programming, one needs an object-


oriented programming language such as Java C++ etc.

The C++ programming language provides a model of memory


and computation that closely matches that of most computers.
In addition, it provides powerful and flexible mechanisms for
abstraction; that is, language constructs that allow the
programmer to introduce and use new types of objects that
match the concepts of an application.

Thus, C++ supports styles of programming that rely on fairly


direct manipulation of hardware resources to deliver a high
degree of efficiency plus higher-level styles of programming
that rely on user-defined types to provide a model of data and
computation that is closer to a human’s view of the task being
performed by a computer. These higher-level styles of
programming are often called data abstraction, object-oriented
programming, and generic programming.

Ans 3

Classes and objects are separate but related concepts. Every


object belongs to a class and every class contains one or more
related objects.

Ø A Class is static. All of the attributes of a class are fixed


before, during, and after the execution of a program. The
attributes of a class don't change.

Ø The class to which an object belongs is also (usually)


static. If a particular object belongs to a certain class at the time
that it is created then it almost certainly will still belong to that
class right up until the time that it is destroyed.

Ø An Object on the other hand has a limited lifespan. Objects


are created and eventually destroyed. Also during that lifetime,
the attributes of the object may undergo significant change.

Ans 4

1.The main difference b/w structure and union is occupying of


size.
2.The size of the union is depending on the element which has
largest size in that union.
3.Where as the size of the structure is sum of all the elements
sizes declared in that structure.

4.The application of structure is to represent different data


items in a continuous memory block.

Ans 5 Member functions can be used to interact with data


contained within user defined types. User defined types provide
flexibility in the "divide and conquer" scheme in program
writing. The two pieces are put together and compiled for
usage. User defined types provide encapsulation defined in the
Object Oriented Programming (OOP) paradigm.

Within classes, to protect the data members, the programmer


can define functions to perform the operations on those data
members. Member functions and functions are names used
interchangeably in reference to classes. Function prototypes are
declared within the class definition. These prototypes can take
the form of non-class functions as well as class suitable
prototypes. Functions can be declared and defined within the
class definition. However, most functions can have very large
definitions and make the class very unreadable. Therefore it is
possible to define the function outside of the class definition
using the scope resolution operator "::". This scope resolution
operator allows a programmer to define the functions
somewhere else. This can allow the programmer to provide a
header file .h defining the class and a .obi file built from the
compiled .cpp file which contains the function definitions. This
can hide the implementation and prevent tampering. The user
would have to define every function again to change the
implementation. Functions within classes can access and modify
(unless the function is constant) data members without
declaring them, because the data members are already declared
in the class.

Ans 6
A scope resolution operator resolves scope ambiguity in cases
where a base class and a derived class both define data member
or function with the same name.

Most generally a scope resolution operator is required when a


data member is redefined by a derived class, or an overriden
method of the derived class wants to call the base class version
of the same method.

Scope resolution:As the name itself indicates,it resolves


global scope to local scope
Ex:
int a=10;
int main()
{
a=20;
cout<<::a<<endl;//prints 20
return 0;
}

Useful to convey ownership of class towards a function


Ex:
class ABC{
public:
static void fun()
{
cout<<"ABC"<<endl;
}
};
class XYZ{
public:
static void fun()
{
cout<<"XYZ"<<endl;
}
};
int main()
{
ABC::fun();
XYZ::fun();
return 0;
}
Ans 7#include<iostream.h>
#include<conio.h>

class student

char name[30],adrs[50]

int rollno;

float mrks[5],total,avg;

public:

void getdata();

void putdata();

};

void student : : getdata()

cout<<"\nEnter the name of Student:";

cin>>name;

cout<<"Enter The Roll No.:";

cin>>rollno;

cout<<"Enter Address:";

cin>>adrs;

cout<<"Enter The marks in Physic's:";

cin>>mrks[0];

cout<<"Enter The marks in Chemistry:";

cin>>mrks[1];

cout<<"Enter The marks in Mathematics:";


cin>>mrks[2];

cout<<"Enter The marks in Hindi:";

cin>>mrks[3];

cout<<"Enter The marks in English:";

cin>>mrks[4];

total=mrks[0]+mrks[1]+mrks[2]+mrks[3]+mrks[4];

avg=total/5;

void student : : putdata()

cout<<"\nName of Student is:"<<name;

cout<<"\nRoll No. is:"<<rollno;

cout<<"\nAddress Is:<<adrs;

cout<<"\nMarks in Physic's:"<<mrks[0];

cout<<"\nMarks in Chemistry:"<<mrks[1];

cout<<"\nMarks in Mathematics:"<<mrks[2];

cout<<"\nMarks in Hindi:"<<mrks[3];

cout<<"\nMarks in English:"<<mrks[4];

cout<<"\n Total Marks obtain:"<<total;

cout<<"\nAverage Marks Obtain:"<<avg;

void main()

int i;
student s[3];

clrscr();

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

s[i].getdata();

s[i].putdata();

getch();

Ans 8 class New

int one;

int two;

int one;

};

Ans:- We can not declare many time a particular variable


and we can also declare same variable at a time also.We
must use member function in it because class is a collection
data member and member function.

class new

int one,two;

public:
void getdata();

void putdata();

};

class item

private:

float x;

char ch;

public:

void getdata();

void putdata();

};

item :: y;

Ans:- We should also use keywords before the class name


while using scope resolulion operator. There is also miss
member function because a class is collection of data member
and member function.There is wrong with the format of scope
resolution operator.

void item :: getdata()


class Sample

private:

int x, y;

public:

void setdata();

void getdata();

void display();

void setdata();

};

Ans:- For the same purpose we do not need to declare many


times member functions

class Sample

private:

int x, y;

public:

void setdata();

void getdata();

};

class Sample1

{
private:

int func;

public:

int func();

Ans:- We can't use declared variables as a member


function.Format problem is also error.

class Sample1

private:

int func;

public:

int setdata();

};

class Sample2

private:

int x, y;

public:

void getdata();

void display();

};

Sample :: void getdata()


{

.....

…..

Ans:- Keywords declared before the class name.

class Sample2

private:

int x, y;

public:

void getdata();

void display();

};

void Sample :: getdata()

.....

…..

class Values

int x, y;
void read() {

cout<< “Enter values: ”<< endl;

cin >> x >> y;

public:

void display() {

cout << “Values entered: ” << x << “ and ” << y <<


endl;

};

void main() {

Values v1;

v1.read();

v1.write();

Ans:- Access should be specified befor member function and


calling of member fuction is wrong with the name.

class Values

int x, y;

public:
void read() {

cout<< “Enter values: ”<< endl;

cin >> x >> y;

void display() {

cout << “Values entered: ” << x << “ and ” << y <<


endl;

};

void main() {

Values v1;

v1.read();

v1.display();

Ans 9

[1]

**200**300

***200***300

****200****300

[2]
Today’s date is

8:9:2008

[3]

Enter information

Record. :1

Rollno:30

Age:18

Sex:m

Record. :2

Infinite

Ans 10# include<iostream.h>

class abc

private:

static int count;

public:

abc()

count=10;

}
void display()

cout<<endl<<”count=” <<count;

};

void main()

abc a1,a2,a3;

a1.display();

a2.display();

a3.display();

# include<iostream.h>

class example

private:

int data;

public:

void exmple();

void display();

};
void example::exmple()

data=0;

void example::display()

cout<<endl<< ”count=” <<count;

void main()

example d;

d.display();

#include <iostream.h>

class item

private:

static int count;

public:

item()

{
count++;

int getcount()

return count;

(Not necessory)

if yes then

int getcountaddresss()

return count;

};

static int item::count;

void main()

item obj1;

item obj2;

cout<< obj1.getcount()<<’ ‘ ;

cout<< obj2.getcount()<< ‘ ‘;
}

Вам также может понравиться