Posts

C# Code to Convert Temperature from degree Celcius to degree Fahrenheit

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Operators_A {     class Program     {         static void Main(string[] args)         {             /*              * 1. Arithmetic Operators              *  a. + (Addition)              *  b. - (Subtraction)              *  c. / (Division)              *  d. * (Multiplication)              *  e. % (Modulus, Remainder)              *  f. ++ (Increment)              *  g. -- (Decrement)              */  ...

Operator Overloading

// Operator Overloading #include <iostream> using namespace std; class Complex { private: float real; float img; public: Complex() { real = img = 0; } Complex(float real, float img) { this->real = real; this->img = img; } // Complex add(Complex c) // { // Complex tmp; // tmp.real = this->real + c.real; // tmp.img = this->img + c.img; // return tmp; // } // Opertors which cannot be oberloaded // ::, ., .*, ?: Complex operator +(Complex c) { Complex tmp; tmp.real = this->real + c.real; tmp.img = this->img + c.img; return tmp; } Complex operator -(Complex c) { Complex tmp; tmp.real = this->real - c.real; tmp.img = this->img - c.img; return tmp; } void display() { cout << this->real << " + " << this->img << "i" << endl; } }; int main() { Complex c1(10.5, 20.5), c2(9.3, 3.5), c3; // c3 = ...

Single Level Inheritance

#include <iostream> using namespace std; /*  * Types:  * 1. Single-Level Inheritance  * 2. Multi-level  * 3. Multiple  * 4. Hybrid  */ class Shape { private:   float width;   float height; public:   Shape()   {     this->width = this->height = 0.0;   }   Shape(float width, float height)   {     this->width = width;     this->height = height;   }   float get_width()   {     return this->width;   }   float get_height()   {     return this->height;   }   float getArea()   {     return this->width * this->height;   } }; class Square : public Shape { public:   Square() { }   Square(float side) : Shape(side, side)   {   }   float getPerimeter()   {     return 2 * this->get_width() + 2 * this-...

Constructors while inheritance

#include <iostream> #include <string.h> using namespace std; class A { public:   A()   {     cout << "A's constructor" << endl;   }   A(string s)   {     cout <<  "A's " << s << endl;   } }; class B : public A { public:   B()   {     cout << "B's constructor" << endl;   }   B(string s) : A(s)   {     cout << "B's " << s << endl;   } }; class C : public B { public:   C()   {     cout << "C's constructor" << endl;   }   C(string s) : B(s)   {     cout << "C's " << s << endl;   } }; int main(int argc, char const *argv[]) {   C c("Hello");   return 0; }

HashMap Example Code

import java . util . HashMap ; import java . util . Map ; import java . util . Iterator ; import java . util . Set ; public class HashMapEx { public static void main ( String args []) { HashMap < Integer , String > hmap = new HashMap < Integer , String >(); // Adding to HashMap hmap . put ( 12 , "Kabir" ); hmap . put ( 2 , "Ravi" ); hmap . put ( 7 , "Luis" ); hmap . put ( 49 , "Peter" ); hmap . put ( 3 , "Aditya" ); // Iterate through HashMap Set set = hmap . entrySet (); Iterator iterator = set . iterator (); while ( iterator . hasNext ()) { Map . Entry mentry = ( Map . Entry ) iterator . next (); System . out . print ( "Key is: " + mentry . getKey () + " & Value is: " ); System . out . println ( mentry . getValue ()); } // Get a value...

Program to Create arraylists for Courses and Students using OOP concepts with getter and setter methods

Course.java public class Course { private String id; private String name; public Course() { } public Course(String id, String name) { this.id = id; this.name = name; } public void setId(String id) { this.id = id; } public String getId() { return this.id; } public void setName(String name) { this.name = name; } public String getName() { return this.name; } @Override public String toString() { return this.id; } } Student.java public class Student { private int rollNo; private String name; private Course course; private String address; private int phoneNumber; /** * Default Constructor */ public Student() { } /** * Overloaded Constructor */ public Student(int rollNo, String name, String address, int phoneNumber, Course course) { this.rollNo = rollNo; this.name = name; this.address = address; this.phoneNumber = phoneNumber; this.course = course; } /** * Setter for rol...

Linear Array using Pointer

#include <stdio.h> #include <stdlib.h> int main(int argc, char const *argv[]) { int *a; // Pointer int size; printf("Size of an array: "); scanf("%d", &size); // malloc()- it allocates the memory dynamically to a pointer variable a = malloc(sizeof(int) * size); printf("Enter %d elements of an array: "); for (int i = 0; i < size; i++) scanf("%d", &a[i]); // Behaving like an array printf("You've entered: "); for (int i = 0; i < size; i++) printf("%d ", a[i]); printf("\n"); return 0; }