Sei sulla pagina 1di 14

PROGRAMMING WITH OBJECT

ORIENTED PARADIGM

Assignment-2

Submitted By:

Name : Manish Kumar Singh


Section : E - 3004
Roll No : RE3004B73
Subject code : Cap320

Submitted To:

Lact. Sucharu Mahajan.


PART A

Q1.How is a class different from a structure and union?

The relationship between class, struct and union


The separating factor between a struct and a union is that a struct can also have
member functions just like a class. The difference between a struct and a class is
that all member functions and variables in a struct are by default public, but in a
class, they default to private as previously discussed.

It is often a good idea to use constructors to initialize the member variables of a


struct. Other than that, though, it is against current standards, and usually looked
down upon, to use functions in a struct, and is usually considered just being lazy.

Difference between a Struct and a Class


The struct type is suitable for representing lightweight objects such as Point,
Rectangle, and Color. Although it is possible to represent a point as a class, a struct
is more efficient in some scenarios. For example, if you declare an array of 1000
Point objects, you will allocate additional memory for referencing each object. In this
case, the struct is less expensive.

When we create a struct object using the new operator, it gets created and the
appropriate constructor is called. Unlike classes, structs can be instantiated without
using the new operator. If you do not use new, the fields will remain unassigned and
the object cannot be used until all of the fields are initialized.

It is an error to declare a default (parameterless) constructor for a struct. A default


constructor is always provided to initialize the struct members to their default values.

It is an error to initialize an instance field in a struct.

There is no inheritance for structs as there is for classes. A struct cannot inherit from
another struct or class, and it cannot be the base of a class. Structs, however, inherit
from the base class Object. A struct can implement interfaces, and it does that
exactly as classes do.

A struct is a value type, while a class is a reference type.

Q2. How is a member function of a class different from a conventional user


defined function? How is the member function of a class accessed?

Member functions

Member functions are operators and functions that are declared as members of a
class. Member functions do not include operators and functions declared with the
friend specifier. These are called friends of a class. You can declare a member
function as static; this is called a static member function. A member function that is
not declared as static is called a nonstatic member function.

The definition of a member function is within the scope of its enclosing class. The
body of a member function is analyzed after the class declaration so that members
of that class can be used in the member function body, even if the member function
definition appears before the declaration of that member in the class member list.
When the function add() is called in the following example, the data variables a, b,
and c can be used in the body of add().
class x
{
public:
int add()
{return a+b+c;};
private:
int a,b,c;
};

User-defined functions

C++ allows programmers to define their own functions. For example the following is
a definition of a function which given the co-ordinates of a point (x,y) will return its
distance from the origin.

float distance(float x, float y) //returns the distance of(x, y) from origin


{
float dist; //local variable
dist= sqrt(x*x+y*y) ;
return dist ;
}

This function has two input parameters, real values x and y, and returns the distance
of the point (x,y) from the origin. In the function a local variable dist is used to
temporarily hold the calculated value inside the function.

The general form of a function definition in C++ is as follows:

function-type function-name( parameter-list )


{

local-definitions;
function-implementation;
}

• If the function returns a value then the type of that value must be specified in
function-type. For the moment this could be int, float or char. If the function does
not return a value then the function-type must be void.

• The function-name follows the same rules of composition as identifiers.

• The parameter-list lists the formal parameters of the function together with
their types.

• The local-definitions are definitions of variables that are used in the function-
implementation. These variables have no meaning outside the function.

• The function-implementation consists of C++ executable statements that


implement the effect of the function.
It is possible to access the class members after a class is defined and objects
are created.

General syntax to access class member:

Object_name.function_name (arguments);

The dot (‘.’) used above is called the dot operator or class member access operator.
The dot operator is used to connect the object and the member function. This
concept is similar to that of accessing structure members in C programming
language. The private data of a class can be accessed only through the member
function of that class.

For example,

A class and object are defined below:

class exforsys
{
int a, b;
public:
void sum(int,int);
} e1;

Then the member access is written as:


e1.sum(5,6);

Where e1 is the object of class exforsys and sum() is the member function of the
class. It is also possible to declare more than one object within a class:

class exforsys
{
private:
int a;
public:
void sum(int)
{
………
………
}
};

main()
In these two objects e1 and e2 are declared of class exforsys.

By default, the access specifier for members of a class is private. The default access
specifier for a structure is public. This is an important difference to recognize and
understand in object-oriented C++ programming language.

class exforsys
{
int x; //Here access specifier is private by default
};
whereas

struct exforsys
{
int x; //Here access specifier is public by default
};

It is not always the case that the member function declaration and definition takes
place within the class itself. Sometimes, declaration of member function alone can
occur within the class. The programmer may choose to place the definition outside
the class. In a situation such as this, it is important to understand the identifying
member function of a particular class. This is performed by using the operator :: this
is called scope resolution operator.

class exforsys
{
private:
int a;
public:
void getvalues() // Only Member Function declaration is done
};
void exforsys :: getvalues() // Here Member Function is defined
{
………………
………………
}

main()
{
exforsys e1,e2;
…………
}
So the usage of scope resolution operator is as follows:

Q3. 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.

In programming, instantiation is the creation of a real instance or particular


realization of an abstraction or template such as a class of objects or a computer
process. To instantiate is to create such an instance by, for example, defining one
particular variation of object within a class, giving it a name, and locating it in some
physical place.

• In object-oriented programming, some writers say that you instantiate a class


to create an object, a concrete instance of the class. The object is an
executable file that you can run in a computer.

• In the object-oriented programming language, Java, the object that you


instantiate from a class is, confusingly enough, called a class instead of an
object. In other words, using Java, you instantiate a class to create a specific
class that is also an executable file you can run in a computer.

• In approaches to data modeling and programming prior to object-oriented


programming, one usage of instantiate was to make a real (data-filled) object
from an abstract object as you would do by creating an entry in a database
table (which, when empty, can be thought of as a kind of class template for
the objects to be filled in).

Yes it is possible when the data of the same class is "public" defined.

class test{
public static void main(String[]ar){
Sub A =new Sub();
Sub B =new Sub();
A.abc=100;
B.abc=A.abc;
System.out.println(B.abc);
}
}
class Sub{
public int abc=0;
}

Explanation:
Here I wrote two classes "test" and "Sub". Inside test class two objects of a class
"Sub" say A and B are created. After that A objects variable abc is assigned by 100.
And A Objects that data is transfered to B objects abc variable and printed out
successfully.
Q4. Can a function be overloaded? If yes then how?

Function overloading is absolutely critical for C++-style template code. If I have to


use different function names for different types, I can't write generic code. That would
eliminate a large and heavily used part of the C++ library, and much of C++'s
functionality.

It's usually present in member function names. A.foo() can call an entirely different
function from B.foo but both functions are named foo It's present in operators, as
+does different things when applied to integers and floating-point numbers, and it's
often used as a string concatenation operator. It seems odd not to allow it in regular
functions as well.

It enables the use of Common Lisp-style "multimethods", in which the exact function
called depends on two data types. If you haven't programmed in the Common Lisp
Object System, try it before you call this useless. It's vital for C++ streams.

I/O without function overloading (or variadic functions, which are worse) would
require a number of different functions, either to print values of different types or to
convert values of different types to a common type (like String).

Without function overloading, if I change the type of some variable or value I need to
change every function that uses it. It makes it much harder to refactor code.

It makes it easier to use APIs when the user doesn't have to remember which type
naming convention is in use, and the user can just remember standard function
names.

Without operator overloading, we'd have to label each function with the types it uses,
if that base operation can be used on more than one type. This is essentially
Hungarian notation, the bad way of doing it.

Overall, it makes a language far more usable.

Function overloading, as opposed to operator overloading, is still not possible in C. C


has no facility to type check data types and use different functions for each. This
cannot be worked around in C other than by writing a function per case and making
an appropriate name change. Then you need to write a specific type identification
case statement and let that make a final call to the correct function. This will,
inefficiently, imitate function overloading as it is available in C++.

bool checkQuestion (bool boolAnswer)


{
if (boolAnswer)
return true;

return false;
}
can be reduced to this:

bool checkQuestion (bool boolAnswer)


{
return boolAnswer;
}

So it's a function that simply returns its parameter.

PART B

Q5.Create 2 classes DM and DB which store the value of distances. DM stores


distances in meters and centimeter and DB store in feet and inches. WAP that
can read values for the class objects and add one object of DM with another
object of Db.
Use the friend function to carry out the addition operation. The object that
stores the results may be a DM object or DB object, depending on the units in
which the results are required.
The display should be in the format of feet and inches or meters and
centimeters depending on the object on display.
#include<iostream.h>
#include<conio.h>
class DB;
class DM
{
public:
int m;
int cm;
void get();
friend void display(DB obj1,DM obj2);

};
class DB
{
public:
int i;
int f;
void get1();
friend void display(DB obj1,DM obj2);
};
void DM::get()
{
cout<<"Enter the distance in meter: ";
cin>>m;
cm=m*100;
}
void DB::get1()
{
cout<<"\nEnter the distance in feet : ";
cin>>f;
i=feet*12;
}

void display(DM obj1,DB obj2)


{
obj1.get();
cout<<"\nDistance in centimeter == "<<obj1.cm;
obj2.getdata1();
cout<<"\nDistance in inch =="<<obj2.i;
}
void main()
{
clrscr();
DM obj1;
DB obj2;
display(obj1,obj2);
getch();
}

Q6. . Write a program to demonstrate how the private data members cannot be
accessed by the public member function of the derived class even though the
derived class has been inherited publicly.

Ans:-
#include<iostream.h>
#include<conio.h>
class base
{
int a;
public:
int b;
void sqr(int c,int d)
{
a=c;
b=d;
cout<<" = "<<a*a;
}
};

class derived:public base


{
int c;
public:
void cube()
{
c=b*b*b;
cout<<"\n cube = "<<x;
}
};
void main()
{
clrscr();
derived z;
int x;
cout<<”enter a number:”;
cin>>x;
cout<<"square and cube of “<<x<<”is:\n\n";
z.sqr(x,x);
z.cube();
getch();
}

Potrebbero piacerti anche