Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Identifying objects and assigning responsibilities to these objects. An object is like Objects communicate to a black box. other objects by sending The internal messages. details are Messages are received hidden. by the methods of an object
1
What is an object?
Tangible Things as a car, printer, ... Roles as employee, boss, ... Incidents as flight, overflow, ... Interactions as contract, sale, ... Specifications as colour, shape,
private data
public processes
+
6
Basic Terminology
Abstraction is the representation of the essential features of an object. These are encapsulated into an abstract data type. Encapsulation is the practice of including in an object everything it needs hidden from other objects. The internal state is usually not accessible by other objects.
7
10
11
Abstract classes
ListClass is an abstract class, i.e., a class in which some methods are left undefined (such as init and append) Abstract classes are not intended to be instantiated, but inherited from Inheritance fills in the blanks by adding the missing methods The result is a concrete class, i.e., a class that can be instantiated since all its methods are defined NilClass and ConsClass are concrete classes This technique is an example of higher-order programming (namely genericity: passing a procedure value, i.e., a method)
12
13
Controlling visibility
Visibility is the control given to the user to limit access to members of a class (attributes and methods) Each member (attribute or method) is defined with a scope (part of program text that the member can be accessed by name) Programming languages use words like public, private, and protected to define visibility Unfortunately, different languages use these keywords to define different scopes
Source of enormous confusion! Be careful!
15
In C++ and Java, a private member is visible among all instances of a given class, but not to subclasses A public member is visible anywhere in the program By default, attributes are private and methods are public
16
17
continue
18
Default Arguments
A default argument is a value given in the function declaration that the compiler automatically inserts if the caller does not provide a value for that argument in the function call. Syntax:
Static Members
22
Continue
23
Function overloading
Function redefined with different set of arguments. EX: add(float, float) Add(int, int) Add (int, int, int) Function overloading is useful when similar function is required to be called with either variable number of arguments or arguments of different type or both.
Function Overloading
Two or more functions can have the same name but different parameters Example:
int max(int a, int b) { if (a>= b) return a; else } return b; float max(float a, float b) { if (a>= b) return a; else return b; }
What is a Friend Function? A friend function is used for accessing the non-public members of a class. A class can allow non-member functions and other classes to access its own private data, by making them friends. Thus, a friend function is an ordinary function or a member of another class. How to define and use Friend Function in C++: The friend function is written as any other normal function, except the function declaration of these functions is preceded with the keyword friend. The friend function must have the class to which it is declared as friend passed to it in argument. Some important points The keyword friend is placed only in the function declaration of the friend function and not in the function definition. It is possible to declare a function as friend in any number of classes. When a class is declared as a friend, the friend class has access to the private data of the class that made this a friend. It is possible to declare the friend function as either private or public. The function can be invoked without the use of an object.
Cont
void PrintDetails()const { cout <<rollno; cout <<name; cout << address; rollno=4 //error } If rollno definition is changed to mutable int rollno; in the student class ,then there will not be an error.
28
Volatile functions
A member function invoked by a volatile object. A volatile object s value can be changed by external parameters which are not under the control of the program.
volatile member functions. Declare a member function with the volatile specifier to ensure that it can be called safely for a volatile object: class B { int x; public: void f() volatile; // volatile member function }; int main() { volatile B b; // b is a volatile object b.f(); // call a volatile member function safely } The object b is declared volatile. Calling a non-volatile member function from this object is unsafe, because b's state might have been changed by a different thread in the meantime. To ensure that f() can be called safely for a volatile object, it's
p 10 x
What is a pointer?
int x = 10; int *p; p = &x; *p = 20; *p is the value at the address p.
p 20 x
What is a pointer?
int x = 10; int *p = NULL; p = &x; *p = 20; * dereference operator gets value at p Declares a pointer to an integer & is address operator gets address of x
A pointer can point to an object created by a class. Object pointers are useful in creating objects at run time. student s1; student *ptr = &s1; s1. getdata(); s1.show(); equivalent to ptr->getdata(); ptr-> show(); or (*ptr).show(); we can also create the objects using pointers and new operator student *ptr = new student; This allocates enough memory for the data members in the object structure and assigns the address of the memory space to ptr. We can also create an array of objects using pointers.
constant objects
const student s1(x,y); // object s1 is constant Any attempt to modify the values of x and y will generate compile time error. A constant object can call only constant member functions. void PrintDetails()const { cout <<rollno; cout <<name; cout << address; }
nested classes
It is an another way of inheriting properties of one class into another. From this we can understand that an object can be collection of many other objects, that is a class can contain objects of other classes as its members . class alpha(); class beta(); class gamma { alpha a1; beta b1; }; All objects of gamma class will contain the objects a1 and b1. This kind of relationship is called
local classes
Classes can be defined and used inside a function or a block. Such classes are called local classes. void test(int a) //function { .. class student //local class {.. .. //class definition }; student s1(a); //create student object . } Local classes can use global variables and static variables declared inside the function. Enclosing function cannot access the private member of a local class.
C++ and C
C is a subset of C++. Advantages: Existing C libraries can be used, efficient code can be generated. But: C++ has the same caveats and problems as C (e.g. pointer arithmetic,). C++ can be used both as a low tlevel n he cus o ects. and as a high level language. sp e fo W v el a i gh l e h
41