C++, C, and C#
Objective #1: Create a simple hello world program in C++
#ifndef CUBE_H #define CUBE_H class Cube { public: Cube(); ~Cube(); void setSide(double s); double getSide(); double area(); double volume(); void toString(); private: double side; }; #endif //Source File cube.cpp #include <iostream>
using namespace std; //#include "cube.h" // constructor Cube::Cube() { } // destructor Cube::~Cube() { } void Cube::setSide(double s) { side = s <= 0 ? 1 : s; } double Cube::getSide() { return side; } double Cube::area() { return 6 * side * side; } double Cube::volume() { return side * side * side; } void Cube::toString() { cout << "Characteristics of this cube"; cout << "\nSide = " << getSide(); cout << "\nArea = " << area(); cout << "\nVolume = " << volume() << "\n\n"; } //Main File: Exo.cpp //#include "cube.h" void main() { Cube cube; cube.setSide(-12.55); cube.toString(); }
Objective #2: Create a simple hello world program in C
Objective #3: Create a simple hello world program in C#
using System; public class Hello { public static int Add(int x, double y) { return x + (int) y; } public static void Main() { int a = 0; double b = 0; string name = ""; double answer = 0; System.Console.Write("Enter your name: "); name = Console.ReadLine(); System.Console.Write("Enter an integer: "); a = int.Parse(Console.ReadLine()); System.Console.Write("Enter a decimal value: "); b = double.Parse(Console.ReadLine()); answer = Add(a, b); System.Console.WriteLine("{0}, the answer is {1}", name, answer); } }
public class Calculation { private int result; //constructor public Calculation() { result = 0; } public Calculation(int x) { result = x; } //destructor ~Calculation() { result = 0; } //methods public void displayResult() { System.Console.WriteLine("Result = {0}", result); } public void add(int x) { result = result + x; } public void subtract(int x) { result = result - x; } } public class Demo { public static void Main() { Calculation myCalculation = new Calculation(90); myCalculation.displayResult(); myCalculation.add(45); myCalculation.displayResult(); myCalculation.subtract(35); myCalculation.displayResult(); myCalculation.subtract(110); myCalculation.displayResult(); } }