290 likes | 405 Views
Introduction to OOP, C#, and Visual Studio. Ch 1, 2, 3. Object-Oriented Programming. Object Abstraction of a read-world item (e.g., car) Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) Class Blueprint for instantiating (creating) objects
E N D
Introduction to OOP, C#, and Visual Studio Ch 1, 2, 3
Object-Oriented Programming • Object • Abstraction of a read-world item (e.g., car) • Has attributes (e.g., size, shape, color) and behaviors (e.g., accelerates, brakes, turns) • Class • Blueprint for instantiating (creating) objects • Many objects may belong to the same class • User-defined, non-built-in type
The OOP Trilogy • Three important OO principles • Encapsulation • Inheritance • Polymorphism
Encapsulation • Attributes & behaviors encapsulated (wrapped) into objects • Information hiding • Implementation details hidden within objects • You can drive a car without knowing details of how engine, transmission really work • Modularization!
Inheritance • Sub classes inherit attributes and behaviors of super class, then add more • A convertible is a car, and has some additional characteristics • Code reuse!
Polymorphism • A behavior or attribute may have many (poly) forms (morph) in different sub classes • Cars with auto or manual transmissions work differently when accelerating. • Method implementing a behavior or attribute in super class may be overridden in a sub class • Extensibility!
OO Analysis & Design • Analyze requirements, then design a solution, from OO point of view • UML (Unified Modeling Language) • A graphical modeling scheme • Prescribes several types of diagrams • Subject of BUS ADM 436
Roots of C# 1973 1985 1995 2000 C C++ Java C# procedural procedural, OO OO object
Procedural vs. OO Languages • Procedural • Unit of program is standalone procedure (also called function, method) • No support of classes and objects • Object-oriented • Unit of program is class for creating objects • Procedures are encapsulated inside classes • No support of standalone procedures • Hybrid, multi-paradigm
C# and Visual Basic • Sister languages • Both are pure OO languages • Virtually equivalent in capabilities • Share the same .NET Framework Class Library • Mainly differ in syntax
.NET and CLR • Microsoft .NET • Independent of specific language or platform • CLR (Common Language Runtime) • Similar to the Java runtime • Runs programs (written in various languages, e.g., C#, VB) pre-compiled into MSIL • Further translates MSIL into machine code for a particular platform
Visual Studio .NET • Integrated Development Environment (IDE) • Supports several languages, including C# and VB • Builds console, GUI, Web-based apps
Visual Basic Syntax ' Fig. 3.1: Welcome1.vb ' Simple Visual Basic program. Module FirstWelcome Sub Main() Console.WriteLine("Welcome to VB!") End Sub ' Main End Module ' FirstWelcome
Developing a Console Application • Create a new project • Choose “Visual C#”, “Console Application” template • Edit source code • Compile (Build Solution) • Fix syntax errors, if any • Run and Debug • Fix logic errors, if any
Code Explanations • Using existing classes in a namespace using System;// contains the Console class • Every application needs at least one class public class Welcome1 • More on public/non-public classes in Ch 10 • Braces { } around a block of code • Every statement ends with a ;
The Main Method static void Main() • Starting point of every C# application • Every application has at least one Main • void method does not return a value • More on staticvs. non-static methods in Ch 7
Variable • Reserved memory cell for storing data • Has name (identifier), type, size (determined by type), and value int number1; • Name: number1 • Type: int • Size: 4 bytes • Value: 0 (default)
Identifier Naming Rules • Consists of letters, digits, underscores • Punctuation characters are not allowed • Cannot start with a digit • Cannot be a keyword, e.g., class, int • C# is case sensitive! (VB is not)
Some Simple Types • bool: 1 byte, true/false • char: 2 bytes, single character • int: 4 bytes, integer • double: 8 bytes, real number • decimal: 16 bytes, monetary value • Other variants with different sizes, ranges
Console Input/Output • Input number1 = int.Parse(Console.ReadLine()); • Convert a string input into a needed type • Output Console.WriteLine($"Sum is {sum}"); • Interpolated string begins with $ • Interpolation expressions enclosed in braces, e.g., {sum}
Summary • OOP • Classes and objects • Encapsulation, inheritance, polymorphism • C# and Visual Basic • Sister languages • Visual Studio • IDE supporting several languages, including C# and Visual Basic