0% found this document useful (0 votes)
4 views

Lab 3

The document discusses classes in C# including declaring classes, creating class objects, adding properties and methods to classes, and using classes in programs. It provides examples of programs that create Book and Student classes, demonstrate value types vs reference types, perform typecasting, and output formatted text using classes.

Uploaded by

Saboor Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Lab 3

The document discusses classes in C# including declaring classes, creating class objects, adding properties and methods to classes, and using classes in programs. It provides examples of programs that create Book and Student classes, demonstrate value types vs reference types, perform typecasting, and output formatted text using classes.

Uploaded by

Saboor Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab #03

Declaration of classes with methods and parameters, loops and


Value Types vs. Reference Types.

Overview of Classes
A Class Is an Active Data Structure

A class is a data structure that can store data and execute code. It contains the following:
• Data members, which store data associated with the class or an instance of the class. Data
Members generally model the attributes of the real-world object the class represents.

• Function members, which execute code. Function members generally model the functions and
actions of the real-world object the class represents.

A C# class can have any number of data and function members. The members can be any Combination
of nine possible member types. These member types are shown in Table 4-1.

Declaring a Class
The class declaration provides the following:
• The class name
• The members of the class
• The characteristics of the class

Task:
1. Write the output of the following program.
using System;

public class Book


{
public string Title;
public string Author;
public short YearPublished;
public int NumberOfPages;
public char CoverType;
}

public class Exercise


{

static void Main()


{
var First = new Book();

First.Title = " Turbo C ";


First.Author = "Robert Lafore";
First.YearPublished = 1996;
First.NumberOfPages = 872;
First.CoverType = 'H';
Console.WriteLine("Book Characteristics");

Console.Write("Title: ");
Console.WriteLine(First.Title);
Console.Write("Author: ");
Console.WriteLine(First.Author);
Console.Write("Year: ");
Console.WriteLine(First.YearPublished);
Console.Write("Pages: ");
Console.WriteLine(First.NumberOfPages);
Console.Write("Cover: ");
Console.WriteLine(First.CoverType);
Console.ReadKey();
}
}

Output:
----------------------------------------------------------------------------------------------------------------

2. Create a class student with a data members name, age, marks of English , marks of
math, marks of science, total marks, obtained marks and percentage provide
member functions CalculateTotalMarks and CalculatePercentage to calculate
marks and percentage in main.

Input:
Output:

-------------------------------------------------------------------------------------------------------

3. Write a program that contains a class which has a method that takes user name as
input and second functions which returns number of vowels present in it and Main
program prints the number of vowels.
Input:

Output:

--------------------------------------------------------------------------------------------------------
4. Write a program that performs typecasting of atleast 3 predefined datatypes in C#.
Input:
O

Output:

---------------------------------------------------------------------------------------------------------

5. Write a C# program that takes a number as input and displays it four times in a row
(separated by blank spaces), and then four times in the next row, with no
separation. You should do it twice: Use the console. Write and use {0}.
Test Data:
Enter a digit: 25
Expected Output:
25 25 25 25
25252525
25 25 25 25
25252525
Input:

Output:

You might also like