The document contains program code snippets to solve various problems:
1. Write programs to print the sum of digits of a number, check if a number is Armstrong, and print prime numbers between 2 and n.
2. Write a program to find the largest and smallest number in an array and sort the array.
3. Write a program to read student details using a class and display the details.
4. Write a program to demonstrate dynamic memory allocation and deallocation using new and delete operators with a class.
5. Write a program to calculate the area of rectangles, circles and squares using constructors.
6. Write a program to implement a copy constructor.
7. Write programs to demonstrate using
The document contains program code snippets to solve various problems:
1. Write programs to print the sum of digits of a number, check if a number is Armstrong, and print prime numbers between 2 and n.
2. Write a program to find the largest and smallest number in an array and sort the array.
3. Write a program to read student details using a class and display the details.
4. Write a program to demonstrate dynamic memory allocation and deallocation using new and delete operators with a class.
5. Write a program to calculate the area of rectangles, circles and squares using constructors.
6. Write a program to implement a copy constructor.
7. Write programs to demonstrate using
The document contains program code snippets to solve various problems:
1. Write programs to print the sum of digits of a number, check if a number is Armstrong, and print prime numbers between 2 and n.
2. Write a program to find the largest and smallest number in an array and sort the array.
3. Write a program to read student details using a class and display the details.
4. Write a program to demonstrate dynamic memory allocation and deallocation using new and delete operators with a class.
5. Write a program to calculate the area of rectangles, circles and squares using constructors.
6. Write a program to implement a copy constructor.
7. Write programs to demonstrate using
The document contains program code snippets to solve various problems:
1. Write programs to print the sum of digits of a number, check if a number is Armstrong, and print prime numbers between 2 and n.
2. Write a program to find the largest and smallest number in an array and sort the array.
3. Write a program to read student details using a class and display the details.
4. Write a program to demonstrate dynamic memory allocation and deallocation using new and delete operators with a class.
5. Write a program to calculate the area of rectangles, circles and squares using constructors.
6. Write a program to implement a copy constructor.
7. Write programs to demonstrate using
b. Check whether the given number is Armstrong or not c. Print the prime number from 2 to n where n is natural number given. 1A………………………………… AIM: To write a CPP program to Print the sum of digits of a given number. PROGRAM: #include <iostream.h> int main( ) { int n,sum=0,m; cout<<"Enter a number: "; cin>>n; while(n>0) { m=n%10; sum=sum+m; n=n/10; } cout<<"Sum is= "<<sum<<endl; return 0; } 1B………………………………… AIM: To write a CPP program to Check whether the given number is Armstrong or not PROGRAM: #include <iostream.h> int main( ) { int n,r,sum=0,temp; cout<<"Enter the Number= "; cin>>n; temp=n; while(n>0) { r=n%10; sum=sum+(r*r*r); n=n/10; } if(temp==sum) cout<<"Armstrong Number."<<endl; else cout<<"Not Armstrong Number."<<endl; return 0; } 1C………………………………… AIM: To write a CPP program to Print the prime number from 2 to n where n is natural number given. PROGRAM: #include<iostream.h> int main( ) { int N, i, j, isPrime, n; cout << "Enter the value of N\n"; cin >> N; // For every number between 2 to N, check // whether it is prime number or not for(i = 2; i <= N; i++) { isPrime = 0; // Check whether i is prime or not for(j = 2; j <= i/2; j++) { // Check If any number between 2 to i/2 divides I // completely If yes the i cannot be prime number if(i % j == 0) { isPrime = 1; break; } } if(isPrime==0 && N!= 1) cout << i << " "; } return 0; } 2. Write a program to find largest and smallest elements in a given list of numbers and sort the given list AIM: To Write a CPP program to find largest and smallest elements in a given list of numbers and sort the given list PROGRAM: #include<iostream.h> int main ( ) { int arr[100], n, i, max, min; cout << "Enter the size of the array : "; cin >> n; cout << "Enter the elements of the array : "; for (i = 0; i < n; i++) cin >> arr[i]; max = arr[0]; for (i = 0; i < n; i++) { if (max < arr[i]) max = arr[i]; } min = arr[0]; for (i = 0; i < n; i++) { if (min > arr[i]) min = arr[i]; } cout << "Largest element : " << max<<endl; cout << "Smallest element : " << min<<endl; return 0; } 3. Write a program to read the student name, roll no, marks and display the same using class and object. AIM: To Write a CPP program to read the student name, roll no, marks and display the same using class and object. PROGRAM: #include <iostream.h> #define MAX 10 class student { private: char name[30]; int rollNo; int total; float perc; public: //member function to get student's details void getDetails(void); //member function to print student's details void putDetails(void); }; //member function definition, outside of the class void student::getDetails(void) { cout << "Enter name: " ; cin >> name; cout << "Enter roll number: "; cin >> rollNo; cout << "Enter total marks outof 500: "; cin >> total; perc=(float)total/500*100; } //member function definition, outside of the class void student::putDetails(void) { cout << "Student details:\n"; cout << "Name:"<< name << ",Roll Number:" << rollNo << ",Total:" << total << ",Percentage:" << perc; } int main( ) { student std[MAX]; //array of objects creation int n,loop; cout << "Enter total number of students: "; cin >> n; for(loop=0;loop< n; loop++) { cout << "Enter details of student " << loop+1 << ":\n"; std[loop].getDetails( ); } cout << endl; for(loop=0;loop< n; loop++) { cout << "Details of student " << (loop+1) << ":\n"; std[loop].putDetails( ); } return 0; } 4. Write a program to implement the dynamic memory allocation and de-allocation using new and delete operators using class and object. AIM: To Write a CPP program to implement the dynamic memory allocation and de- allocation using new and delete operators using class and object. PROGRAM: #include <iostream.h> int main( ) { // declare an int pointer int* pointInt; // declare a float pointer float* pointFloat; // dynamically allocate memory pointInt = new int; pointFloat = new float; // assigning value to the memory *pointInt = 45; *pointFloat = 45.45f; cout << *pointInt << endl; cout << *pointFloat << endl; // deallocate the memory delete pointInt; delete pointFloat; return 0; } 5. Write a program to find area of a rectangle, circle, and square using constructors. AIM: To Write a CPP program to find area of a rectangle, circle, and square using constructors. PROGRAM: #include <iostream.h> class Area { public: Area(int length, int breadth) /*Creating a constructor with 2 parameter*/ { cout << "Area of the rectangle =" << length * breadth << endl; } Area(double r) { cout << "Area of the circle "<<3.14*r*r << endl; } Area(int side) /*Creating a constructor with 1 parameter*/ { cout << "Area of the square = " << side * side << endl; } }; int main( ) { Area obj2(5, 6); /*Initializing the value of length=5 and breadth=6*/ Area obj1(5.0);/*Initializing the value of radius of circle=5*/ Area obj3(5); /*Initializing the value of side of square=5*/ return (0); } 6. Write a program to implement copy constructor. AIM: To Write a CPP program to implement copy constructor. PROGRAM: #include<iostream.h> class Point { private: int x, y; public: Point(int x1, int y1) { x = x1; y = y1; } // Copy constructor Point(const Point &p1) { x = p1.x; y = p1.y; } int getX( ) { return x; } int getY( ) { return y; } }; int main( ) { Point p1(10, 15); // Normal constructor is called here Point p2 = p1; // Copy constructor is called here // Let us access values assigned by constructors cout << "p1.x = " << p1.getX() << ", p1.y = " << p1.getY( ); cout << "\np2.x = " << p2.getX() << ", p2.y = " << p2.getY( ); return 0; }
7. Write a program using friend functions and friend class.
7A………………………… AIM : To Write a CPP Program using friend function #include <iostream.h> class Box { private: int length; public: Box( ): length(0) { } friend int printLength(Box); //friend function }; int printLength(Box b) { b.length += 10; return b.length; } int main( ) { Box b; cout<<"Length of box: "<< printLength(b)<<endl; return 0; } 7B…………………….. AIM: To Write a CPP Program using friend function #include <iostream.h> class A { int x =5; friend class B; // friend class. }; class B { public: void display(A &a) { cout<<"value of x is : "<<a.x; } }; int main( ) { A a; B b; b.display(a); return 0; } 8. Write a program to implement constructors a. Default Constructor, Parameterized Constructor, Copy Constructor b. Define the constructor inside/outside of the class c. Implement all three constructors within a single class as well as use multiple classes( individual classes) 8A………………. DEAFULT CONSTRUCTOR AIM: To write a CPP program to demonstrate Default constructor PROGRAM: #include <iostream.h> class Employee { public: Employee( ) { cout<<"Default Constructor Invoked"<<endl; } }; int main(void) { Employee e1; //creating an object of Employee Employee e2; return 0; } PARAMETERIZED CONSTRUCTOR: AIM: To write a CPP program to demonstrate Parameterized constructor PROGRAM: #include <iostream.h> class Employee { public: int id;//data member (also instance variable) string name;//data member(also instance variable) float salary; Employee(int i, string n, float s) { id = i; name = n; salary = s; } void display( ) { cout<<id<<" "<<name<<" "<<salary<<endl; } }; int main(void) { Employee e1 =Employee(101, "Sonoo", 890000); //creating an object of Employee Employee e2=Employee(102, "Nakul", 59000); e1.display(); e2.display(); return 0; } COPY CONSTRUCTOR: AIM: To write a CPP program to demonstrate Copy constructor PROGRAM: #include <iostream.h> class A { public: int x; A(int a) // parameterized constructor. { x=a; } A(A &i) // copy constructor { x = i.x; } }; int main( ) { A a1(20); // Calling the parameterized constructor. A a2(a1); // Calling the copy constructor. cout<<a2.x; return 0; } 8B………………………. AIM: To Write a CPP Program to define a constructor inside of the class PROGRAM: #include <iostream.h> class Employee { public: Employee( ) { cout<<"Default Constructor Invoked"<<endl; } }; int main(void) { Employee e1; //creating an object of Employee Employee e2; return 0; }
AIM: To Write a CPP Program to define a constructor outside of the class
PROGRAM: #include <iostream.h> #include<conio.h> // Class Declaration class Example { int a, b; //Access - Specifier public: //Constructor declaration Example( ); //Member Functions for display 'a & b' Values. void Display( ) { cout << "Values :" << a << "\t" << b; } }; // Constructor definition outside Class Example::Example( ) { // Assign Values In Constructor a = 10; b = 20; cout << "Im Constructor : Outside Class\n"; } int main( ) { // Object Creation For Class Example Object; // Constructor invoked. Object.Display( ); // Wait For Output Screen getch( ); return 0; } 8C…………. AIM: To write a CPP program to demonstrate all type of constructors in one class #include<iostream.h> class A { public: int x; A( ) { cout<<"defalt constructor is called "<<endl; } A(int r) { x=r; cout<<"Paremeterized constructor"<<endl; cout<<"paremeter values"<<x<<endl; } A(A &i) { x=i.x; cout<<"Copy constructor"<<endl; cout<<"copied parameter vallue"<<x<<endl; } }; int main( ) { A a1; A a2(10); A a3(a2); } AIM: To write a CPP program to demonstrate all type of constructors in different class PROGRAM: #include<iostream.h> class A { public: A( ) { cout<<"defalt constructor is called "<<endl; } }; class B { public: int x; B(int r) { x=r; cout<<"Paremeterized constructor"<<endl; cout<<"paremeter values"<<x<<endl; } }; class C { public: int x; C(B &i) { x=i.x; cout<<"Copy constructor"<<endl; cout<<"copied parameter vallue"<<x<<endl; } }; int main( ) { A a1; B b1(10); C c1(b1); } 9. Write a program to implement the following concepts using class and object a. Function overloading b. Operator overloading (unary/binary(+ and -)) 9A:FUNCTION OVERLOADING AIM: To write a CPP program to demonstrate function overloading using a class and object PROGRAM: #include <iostream.h> class Cal { public: int add(int a,int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; int main(void) { Cal C; cout<<C.add(10, 20)<<endl; cout<<C.add(12, 20, 23); return 0; } 9b. Operator overloading (unary/binary(+ and -)) UNARY OPERATOR OVERLOADING AIM: To write a CPP program to demonstrate unary Operator overloading using a class and object PROGRAM: #include <iostream.h> class Distance { public: // Member Object int feet, inch; // Constructor to initialize the object's value Distance(int f, int i) { this->feet = f; this->inch = i; } // Overloading(-) operator to perform decrement // operation of Distance object void operator-( ) { feet--; inch--; cout << "\nFeet & Inches(Decrement): " << feet << "'" << inch; } }; // Driver Code int main( ) { // Declare and Initialize the constructor Distance d1(8, 9); // Use (-) unary operator by single operand -d1; return 0; } BINARY OPERATOR OVERLOADING AIM: To write a CPP program to demonstrate Binary Operator overloading using a class and object PROGRAM: #include <iostream.h> class Arith_num { // declare data member or variable int x, y; public: // create a member function to take input void input( ) { cout << " Enter the first number: "; cin >> x; } void input2( ) { cout << " Enter the second number: "; cin >> y; } // overloading the binary '+' operator to add number Arith_num operator + (Arith_num &ob) { // create an object Arith_num A; // assign values to object A.x = x + ob.x; return (A); } // display the result of binary + operator void print( ) { cout << "The sum of two numbers is: " <<x; } }; int main ( ) { Arith_num x1, y1, res; // here we create object of the class Arith_num i.e x1 and y1 // accepting the values x1.input( ); y1.input( ); // assign result of x1 and x2 to res res = x1 + y1; // call the print( ) function to display the results res.print( ); return 0; } 10. Write a program to demonstrate single inheritance, multilevel inheritance and multiple inheritances 10A…… AIM: To Write a CPP program to demonstrate Single Inheritence. PROGRAM: #include <iostream.h> class Account { public: float salary = 60000; }; class Programmer: public Account { public: float bonus = 5000; }; int main(void) { Programmer p1; cout<<"Salary: "<<p1.salary<<endl; cout<<"Bonus: "<<p1.bonus<<endl; return 0; } 10B…….. AIM: To write a CPP program to demonstrate the Multilevel Inheritance PROGRAM: #include <iostream.h> class Animal { public: void eat( ) { cout<<"Eating..."<<endl; } }; class Dog: public Animal { public: void bark( ) { cout<<"Barking..."<<endl; } }; class BabyDog: public Dog { public: void weep( ) { cout<<"Weeping..."; } }; int main(void) { BabyDog d1; d1.eat( ); d1.bark( ); d1.weep( ); return 0; } 10C……………. AIM: To write a CPP program to demonstrate Multiple Inheritance PROGRAM: #include <iostream.h> class A { protected: int a; public: void get_a(int n) { a = n; } }; class B { protected: int b; public: void get_b(int n) { b = n; } }; class C : public A,public B { public: void display( ) { std::cout << "The value of a is : " <<a<< std::endl; std::cout << "The value of b is : " <<b<< std::endl; cout<<"Addition of a and b is : "<<a+b; } }; int main( ) { C c; c.get_a(10); c.get_b(20); c.display( ); return 0; } 11. Write a program to implement the overloaded constructors in inheritance. AIM: Write a CPP program to implement the overloaded constructors in inheritance. PROGRAM: #include<iostream.h> class A { public: int x; A( ) { cout<<"defalt constructor is called "<<endl; } A(int r) { x=r; cout<<"Paremeterized constructor"<<endl; cout<<"paremeter values"<<x<<endl; } A(A &i) { x=i.x; cout<<"Copy constructor"<<endl; cout<<"copied parameter vallue"<<x<<endl; } }; int main( ) { A a1; A a2(10); A a3(a2); } 12. Write a program to implement the polymorphism and the following concepts using class and object. a. Virtual functions b. Pure virtual functions 12A……….. AIM: To write a CPP program to implement the polymorphism using Virtual functions PROGRAM: #include<iostream.h> class B { public: virtual void s( ) { cout<<" In Base \n"; } }; class D: public B { public: void s( ) { cout<<"In Derived \n"; } }; int main(void) { D d; // An object of class D B *b= &d; // A pointer of type B* pointing to d b->s( ); // prints "D::s() called" return 0; } 12B………………….. AIM: To write a CPP program to implement the polymorphism using Pure Virtual functions PROGRAM: #include<iostream.h> class B { public: virtual void s( ) = 0; // Pure Virtual Function }; class D:public B { public: void s( ) { cout << "Virtual Function in Derived class\n"; } }; int main( ) { B *b; D dobj; b = &dobj; b->s( ); } 13. Write a program to implement the virtual concepts for following concepts a. Constructor (not applied) b. Destructor (applied) 13A. AIM: To Write a CPP program to implement the virtual concepts with Constructor (not applied) PROGRAM: 13B: AIM: To Write a CPP program to implement the virtual concepts with Destructor(applied) PROGRAM: #include<iostream.h> class b { public: b( ) { cout<<"Constructing base \n"; } virtual ~b( ) { cout<<"Destructing base \n"; } }; class d: public b { public: d( ) { cout<<"Constructing derived \n"; } ~d( ) { cout<<"Destructing derived \n"; } };
int main(void) { d *derived = new d( ); b *bptr = derived; delete bptr; return 0; }
14. Write a program to demonstrate static polymorphism using method overloading.
AIM: Write a CPP program to demonstrate static polymorphism using method overloading. PROGRAM: #include <iostream.h> class Cal { public: int add(int a, int b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } }; int main(void) { Cal C; cout<<C.add(10, 20)<<endl; cout<<C.add(12, 20, 23); return 0; }
15. Write a program to demonstrate dynamic polymorphism using method overriding
and dynamic method dispatch. AIM: Write a CPP program to demonstrate dynamic polymorphism using method overriding and dynamic method dispatch. PROGRAM: #include <iostream.h> class Base { public: void print( ) { cout << "Base Function" << endl; } }; class Derived : public Base { public: void print( ) { cout << "Derived Function" << endl; } }; int main( ) { Derived derived1, derived2; derived1.print( ); // access print( ) function of the Base class derived2.Base::print( ); return 0; } 16. Write a program to implement the template (generic) concepts a. Without template class and object b. With template class and object 16A. AIM: To write a CPP Program to demonstrate a program without Template class and object PROGRAM: #include<iostream.h> int square(int x) { return x * x; } float square(float x) { return x * x; } double square(double x) { return x * x; } int main( ) { int a,b; float i,j; double x,y; a=2; i=2.5; x=3.75; b=square(a); cout<<"square of a"<<b<<endl; j=square(i); cout<<"square of i"<<j<<endl; x=square(y); cout<<"square of x"<<y<<endl; } 16B. AIM: To write a CPP Program to demonstrate a program with Template class and object PROGRAM: #include<iostream.h> template<class T1> class square { T1 x; public: void getdata( ) { cin>>x; } void display( ) { cout<<"result="<<x*x<<endl; } }; int main( ) { square<int> obj1; square<float> obj2; square<double> obj3; cout<<"enter an integer vlaue"<<endl; obj1.getdata( ); obj1.display( ); cout<<"enter an float vlaue"<<endl; obj2.getdata( ); obj2.display( ); cout<<"enter an double vlaue"<<endl; obj3.getdata( ); obj3.display( ); return 0; }