>name>>rollno; } void show() { cout<<"Rollno:\t"< Download presentation Presentation is loading. Please wait. Published byAmelia Lyons
Modified over 9 years ago
2
Protectedly Inherited Base Class When the access specifier of the base class in the derived class definition is protected, the base class is protectedly inherited. Both the protected and public member of base class becomes the protected member of derived class.
3
//Program to demonstrate protectedly inherited base class. #include class student { int rollno; char name[10]; public: void get() { cout<<"Enter name and rollno"; cin>>name>>rollno; } void show() { cout<<"Rollno:\t"<<rollno<<"Name:\t"<<name; } };
4
class result:protected student { public: int marks; void get1() { get(); cout<<"Enter marks:\n"; cin>>marks; } void show1() { show(); cout<<"MArks:"<<marks; } };
5
void main() { result r; clrscr(); r.get1(); r.show1(); getch(); }
6
Multiple Inheritance When a derived class inherits from more than one base class simultaneously, it is referred to as multiple inheritance, the derived class inherits all the members of base class and can directly access the public and protected members of the base class.
7
#include class person { char name[30]; int age; public: void getdata() { cout<<"Enter name and age:"; cin>>name>>age; } void showdata() { cout<<"Name:\n"<<name; cout<<"Age:\n"<<age; } };
8
class employee { int salary; public: void getsal() { cout<<"Enter salary:"; cin>>salary; } void showsal() { cout<<"\nsalary:"<<salary; } }; class fulltime:public person, public employee { };
9
void main() { clrscr(); fulltime f; cout<<"Enter employees data:"; f.getdata(); f.getsal(); cout<<"\n\nEmployees Info:"; f.showdata(); f.showsal(); getch(); }
10
Ambiguity Resolution in Multiple Inheritance In multiple inheritance an ambiguity may arise when two or more base classes have a member with same name. In order to resolve such ambiguities, the name of the member is qualified with the name of the base class by using the scope resolution operator.
11
#include class person { char name[30]; int age; public: void getdata() { cout<<"Enter name and age:"; cin>>name>>age; } void showdata() { cout<<"Name:\n"<<name; cout<<"Age:\n"<<age; } };
12
class employee { int salary; public: void getdata() { cout<<"Enter salary:"; cin>>salary; } void showsal() { cout<<"\nsalary:"<<salary; } }; class fulltime:public person, public employee { };
13
void main() { clrscr(); fulltime f; cout<<"Enter employees data:"; f.getdata(); //Ambiguity cout<<"\n\nEmployees Info:"; f.showdata(); f.showsal(); getch(); }
14
Resolving Ambiguity #include class person { char name[30]; int age; public: void getdata() { cout<<"Enter name and age:"; cin>>name>>age; } void showdata() { cout<<"Name:\n"<<name; cout<<"Age:\n"<<age; } };
15
class employee { int salary; public: void getdata() { cout<<"Enter salary:"; cin>>salary; } void showsal() { cout<<"\nsalary:"<<salary; } }; class fulltime:public person, public employee { };
16
void main() { clrscr(); fulltime f; cout<<"Enter employees data:"; f.person::getdata(); f.employee::getdata(); cout<<"\n\nEmployees Info:"; f.showdata(); f.showsal(); getch(); }
17
Hierarchical Inheritance Hierarchical Inheritance is a type of inheritance in which more than one class is derived from a single base class. In hierarchical inheritance a base class provides members that are common to all of its derived class. In hierarchical inheritance each of the derived class inherits all the member of its base class. In addition all derived classes can directly access the public and protected members of base class. However one derived class cannot access the members of another derived class.
19
#include class employee { char name[30]; int age; public: void getdata() { cout<<"\nEnter name and age:"; cin>>name>>age; } void showdata() { cout<<"Name:\t"<<name<<"\tAge:\t"<<age; } };
20
class fulltime:public employee { int salary; public: void getsal() { cout<<"\nENter salary"; cin>>salary; } void showsal() { cout<<"\nsalary"<<salary; } };
21
class contract:public employee { int hrs_worked; int wages_perhr; public: void calsal() { cout<<"\nENter number of hours worked:"; cin>>hrs_worked; cout<<"\nENter wages per hour"; cin>>wages_perhr; } void showcalsal() { cout<<"\nTotalsalary:"<<hrs_worked*wages_perhr; } };
22
void main() { clrscr(); fulltime f; cout<<"Enter fulltime employee data:"; f.getdata(); f.getsal(); cout<<"\nEnter contract employee data:"; contract c; c.getdata(); c.calsal(); cout<<"\nFulltime employee info:"; f.showdata(); f.showsal(); cout<<"\ncontract employees detail:"; c.showdata(); c.showcalsal(); getch(); }
23
Virtual Function Virtual function in c++ support run time polymorphism. A virtual function is a member function that is declared inside the base class and its functionality can be overridden in derived classes. When a base class containing virtual function is inherited, the derived class may implement their own versions of that function. The entire function of base class can be replaced by a set of new implementation in derived class. This states that base class provides common interface and this interface can be implemented in different ways in different derived classes.
24
Virtual Function A member function can be made virtual by prefixing its declaration with the keyword virtual in the base class For Example: Virtual void show() Whenever a virtual function is inherited its virtual nature is also inherited into all its derived classes and it is not required to use the keyword virtual in the subsequent derived classes. A virtual function always remains virtual irrespective of the number of times it is inherited. A virtual function must be accessed using a pointer of base class type and not of derived class type
25
Virtual Functions Base b; //object of base class Derived d;//object of derived class Base *bptr;//pointer of base class Bptr = &b;//pointing to base class bptr- show();//show() of base class Bptr=&d;//pointing to derived class Bptr=show();//show() of derived class
26
//Program to demonstrate virtual function #include class base { public: virtual void display() { cout<<"\nDisplay of base is called"; } }; class derived:public base { public: void display() { cout<<"\nDisplay of derived class called"; } };
27
void main() { clrscr(); base b; derived d; base *bptr; bptr= &b; bptr->display(); bptr= &d; bptr->display(); }
28
File Handling The present day computers are used to store very large amount of data. All this data is stored in devices such as floppy disk or hard disk, compact disk. Data is stored in these devices using a concept of files. A file is a collection of related data stored in a particular area on the disk. Files from where you can access data randomly are called random access files. The other form of storing files is sequential files in which data is stored sequentially.
29
Operation with a file The different file operations are (i)Assigning a name to the file:-Every file has a name to identify in its directory. (ii) Opening the file:-It may involve opening a new file if it does not exist, or opening an existing file. (iii) File processing:-we may read, write, delete, modify some data. (iv) Deleting Errors:-Sometimes few operations are not carried out due to some cause and that must be rectified. (v)Closing the file:-after processing file must be closed.
30
Classes for file stream operations In c++ classes for file stream operations are:- (i)Ifstream (ii)Ofstream (iii) fstream These classes are derived from iostream.
31
Opening and closing of a file For opening a file we must create a file stream and then link it to the file name. The class to be used depends upon the purpose,that is whether we want to read data from file or write data to it A file can be opened in two ways: - Using constructor of the class. - Using open() function of the class. (i)Opening files using constructor: The first operation generally performed on file object is to associate it to real life.
32
This procedure is called as to “open a file”. An open file is represented within a program by a stream object and any input or output operation performed on this stream object. In order to open a file with a stream object, we use its constructor function as ofstream fwrite (“filename.txt”);
33
// Program to demonstrate opening a file using constructor function #include void main() { clrscr(); ofstream fwrite("one.txt"); fwrite<<"this is first line in the file"; fwrite.close(); getch(); }
34
Opening file using open() function In order to open a file with a stream object, we use its open function as ofstream fwrite fwrite.open(“filename.txt”); Open() function has taken one argument, “filename.txt”. If this file exist, it opens in writing mode otherwise it creates a new one. //program to demonstrate opening a file using open() function //Program to demonstrate input and output(reading and writing) to file in the same program
35
#include Void main() { Clrscr(); Char data[80]; Ifstream fread; Fread.open(“one.txt”); While(!(fread.eof())) { fread.getline(data,80); cout<<data<<endl; } Fread.close(); Getch(); }
36
Closing a file When we are finished with our input and output operations on a file we have to close it so that resources become available again. To close a file we have to call the member function close() of stream classes. For example fwrite.close() fread.close() This member function doesnot take any parameter and it flushes the associated buffer first and then closes the file.
37
Detecting End of file Detection of end of file condition is necessary for preventing any further attempt to read data from the file. An ifstream object such as fread returns a value 0 if any error occurs in the file operation including the end of file condition. Thus the while loop terminates when fread returns a value 0 on reaching the end of file condition. There is another approach to detect end of file condition while(!fread.eof())
38
#include Void main() { Clrscr(); Char data[80]; Ifstream fread; Fread.open(“one.txt”); While(!(fread.eof())) { fread.getline(data,80); cout<<data<<endl; } Fread.close(); Getch(); }
39
#include void main() { clrscr(); char data[80]; ofstream fwrite("student.txt"); fwrite<<"Rollno\t\t Name\t\t Percentage"<<endl; fwrite<<"101\t\t\t\t sagar\t\t 80%"<<endl; fwrite<<"102\t\t\t neha\t\t 45%"<<endl; fwrite.close(); ifstream fread("student.txt"); while(fread) { fread.getline(data,80); cout<<data<<endl; } fread.close(); getch(); }
40
Sequential Input and output operations The file stream classes support a number of member functions for performing the input and output operations on file. We are going to use two pairs (i) Put() and get() single:-Are designed for handling single character a time. (ii) Write() and read():- Are designed for handling write and read blocks for binary data.
Similar presentations © 2024 SlidePlayer.com. Inc. Log in
Protectedly Inherited Base Class When the access specifier of the base class in the derived class definition is protected, the base class is protectedly.
Similar presentations
Presentation on theme: "Protectedly Inherited Base Class When the access specifier of the base class in the derived class definition is protected, the base class is protectedly."— Presentation transcript:
Similar presentations
All rights reserved.