Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
ORIENTED PARADIGM
Assignment-2
Submitted By:
Submitted To:
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.
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.
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.
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.
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 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.
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,
class exforsys
{
int a, b;
public:
void sum(int,int);
} e1;
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.
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?
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.
return false;
}
can be reduced to this:
PART B
};
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;
}
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;
}
};