Riassunto di Programmazione a oggettiFull description
Stefan Priebsch's slides from Codeworks 2009Full description
FUNCTION OVERLOADING AIM:
To write a C++ program to perform function overloading. ALGORITHM:
Step1: Start the program. Step2: Define a class with necessary data items and member function with same
function name and different number and types of arguments. Step3: Define a member function to calculate the volume of different shapes. Step4: Define the main function. Step5: Call the member function using the object created. Step6: Print the result. Step7: Stop the program. PROGRAM:
#include #include class overloading { private: int s,r,l,b,h; public: int volume(int s) { return(s*s*s); } long volume(int l,int b,int h) { return(l*b*h); }
Output is Volume=1000 Volume=50.240002 Volume=7875
CS2312-OBJECT ORIENTED PROGRAMMING LAB
FUNCTION OVERLOADING WITH DEFAULT ARGUMENT AIM:
To write a C++program to perform function overloading with default argument ALGORITHM:
Step1: Start the program. Step2: Declare a function printline which takes a character and integer as argument. Step3:Set a loop using variable “i” and initialize it to zero. Check if “i”
#include #include void printline(char='_',int=7); void main() { clrscr(); printline(); printline('!'); printline('*',4); printline('r',5); getch(); } void printline(char ch,int count) {
CS2312-OBJECT ORIENTED PROGRAMMING LAB
int i; cout<
OUTPUT: _______ !!!!!!! **** rrrrr
CS2312-OBJECT ORIENTED PROGRAMMING LAB
SIMPLE CLASS DESIGN IN C++, NAMESPACE, OBJECT CREATION AIM:
To write a C++ program to design a simple class. ALGORITHM:
Step1: Start the program. Step2: Define a class with necessary data items and member function. Step3: Define the main function. Step4: Create an object for the class and call the member functions using that object. Step5: Print the result. Step6: Stop the program.
PROGRAM:
#include using namespace std; class arithmetic { private: int a,b,c; float d; public: void input() { cout<<"Enter 2 Numbers:"; cin>>a>>b; } void manipulate() { c=a+b;
CLASS DESIGN IN C++ USING DYNAMIC MEMORY ALLOCATION, DESTRUCTOR, COPY CONSTRUCTOR AIM:
To write a C++ program to print matrix with dynamic memory allocation, copy constructor, destructor and operator overloading. ALGORITHM:
Step1: Start the program . Step2: The matrix class is declared. Step3: In the main function the size of matrix is declared and the values are entered. Step4: Using copy constructor values are accessed and memory is allocated dynamically for rows and columns and matrix is displayed. Step5: The assignment operator is overloaded and the matrix is printed. Step6: The dynamically allocated memory can be deallocated with the help of destructor. Step6: Stop the program.
PROGRAM:
#include #include class matrix { int **m; int row,col; public: matrix() {
row=m2.row; col=m2.col; m=new int*[row]; for(int i=0;i>m[i][j]; } void matrix::showmatrix() { for(int i=0;i>r>>c; matrix m1(r,c); cout<<"\n Enter the matrix elements one by one….\n"; m1.getmatrix(); cout<<"\n Entered matrix is….\n"; m1.showmatrix(); matrix m2=m1; cout<<"\nResult of copy constructor is….\n"; m2.showmatrix();
CS2312-OBJECT ORIENTED PROGRAMMING LAB
matrix m3; m3=m1; cout<<"\n Result of assignment operator overloading.\n"; m3.showmatrix(); getch(); }
CS2312-OBJECT ORIENTED PROGRAMMING LAB
OUTPUT:
Enter the rows and cols of the matrix…. 3 3 Enter the matrix elements one by one…. 1 2 3 4 5 6 7 8 9 Entered matrix is…. 1 4 7
2 5 8
3 6 9
Copy Constructor invoked…. Result of copy constructor is…. 1 4 7
2 5 8
3 6 9
Assignment Operator overloading…. Result of assignment operator overloading…. 1 4
2 5
3 6
CS2312-OBJECT ORIENTED PROGRAMMING LAB
7
8
9 OPERATOR OVERLOADING USING FRIEND FUNCTION
AIM:
To write a C++ program to import operator overloading with friend function concept. ALGORITHM: Step1: Start the program.
Step2: Create a class complex with 3 constructor to perform constructor overloading. The first constructor takes no argument which is used to create objects which are not initialized. Step3: The second takes one argument which is used to create object and initialize them to specific value. Step4: Overload the arithmetic operators using friend function to perform arithmetic operation on the complex number. Step 5: Stop the program PROGRAM:
The imag part of complex number: 220226.465795 INHERITANCE AIM:
To write a C++ program to implement bank details using inheritance concept. ALGORITHM:
Step1: Start the program. Step2: Create a bank class. Step3: Create a subclass savings which inherits properties from the bank class and current class that inherits from savings class. Step4: The savings class defines member function to get details about the deposit and withdrawal amount and update balance information for current account. Step5: Write a member function to get deposit amount and to update balance information for current account. Step6: Write a member function to display the balance information for saving account. Step7: Write a member function to display the balance information for respective account type. Step8: Stop the program. PROGRAM: #include #include class bank { public: char *cname; long int accno;
CS2312-OBJECT ORIENTED PROGRAMMING LAB
void display() { cout<>dep; bal+=dep; cout<<"\nEnter the amount to be withdrawn=Rs."; cin>>wdraw; bal-=wdraw; cout<<"\nYour account balance is=Rs."<>a.cname; cout<<"\n Enter your A/c no.:";
CS2312-OBJECT ORIENTED PROGRAMMING LAB
cin>>a.accno; cout<>ch; switch(ch) { case 1: a.saves(); break; case 2: a.info(); break; case 3: break; default: cout<<"Invalid choice"; break; } if(ch==3) break; } getch(); } OUTPUT: Enter customer name:xxx Enter your A/c no.:566 Enter the A/c type:1.Savings 2.Current 3.Exit 1 Enter the amount to be deposited=Rs.5000 Enter the amount to be withdrawn=Rs.500 Your A/c balance is=Rs.4500 Enter the A/c type:1.Savings 2.Current 3.Exit 2 Last amount withdrawn=Rs.500 Last amount deposited=Rs.5000 Your account balance is=Rs.4500
CS2312-OBJECT ORIENTED PROGRAMMING LAB
Enter the A/c type:1.Savings 2.Current 3.Exit 3 RUNTIME POLYMORPHISM USING VITUAL FUNCTION AIM:
To write a C++ program to calculate the area of triangle and rectangle using derived classes and display the result using virtual function. ALGORITHM:
Step1: Start the program. Step2: Create a base class shape. Step3: Derive two subclasses triangle and rectangle from the base class shape. Step4: Derive member function get_data() and display_area(). Step5: Find out the area of triangle and rectangle and display the result using display_area(). Step6: Make display_area() as a virtual function. Step7: Print the result. Step8: Stop the program. PROGRAM:
#include #include class shape { public: virtual void getdata(){} virtual void display(){} }; class triangle: public shape { int h,bs; float at;
CS2312-OBJECT ORIENTED PROGRAMMING LAB
public: void getdata() { cout<<"Enter the height and base of triangle = "; cin>>h>>bs; } areatri() { at=((bs*h)/2.0); return(at); } void display() { cout<<"Area of triangle= "<>l>>b; } arearec() { ar=l*b; return(ar); } void display() { cout<<"Area of rectangle= "<
CS2312-OBJECT ORIENTED PROGRAMMING LAB
Triangle Enter the height and base of triangle = 4 6 Area of triangle=12 sq.units Rectangle Enter the length and breadth of rectangle = 78 Area of rectangle=56 sq.units
CS2312-OBJECT ORIENTED PROGRAMMING LAB
TEMPLATE DESIGN IN C++ AIM:
To write a C++ program to print the Student Details. ALGORITHM: