Friday, March 29, 2024
05:19 PM (GMT +5)

Go Back   CSS Forums > CSS Optional subjects > Group I > Computer Science

Reply Share Thread: Submit Thread to Facebook Facebook     Submit Thread to Twitter Twitter     Submit Thread to Google+ Google+    
 
LinkBack Thread Tools Search this Thread
  #1  
Old Thursday, January 24, 2008
Janeeta's Avatar
Member
 
Join Date: Dec 2006
Location: Karachi
Posts: 96
Thanks: 26
Thanked 121 Times in 39 Posts
Janeeta is on a distinguished road
Thumbs up C++ (Computer science)

Inheritance

A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class
The idea of inheritance is simple but powerful:
When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass

An Example of Inheritance
Here is the sample code for a possible implementation of a Bicycle class that was presented in the Classes and Objects lesson:
public class Bicycle {

// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;

// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}

// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}

public void setGear(int newValue) {
gear = newValue;
}

public void applyBrake(int decrement) {
speed -= decrement;
}

public void speedUp(int increment) {
speed += increment;
}

}
A class declaration for a MountainBike class that is a subclass of Bicycle might look like this:
public class MountainBike extends Bicycle {

// the MountainBike subclass adds one field
public int seatHeight;

// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}

// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}

}
MountainBike inherits all the fields and methods of Bicycle and adds the field seatHeight and a method to set it.

What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
· The inherited fields can be used directly, just like any other fields.
· You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
· You can declare new fields in the subclass that are not in the superclass.
· The inherited methods can be used directly as they are.
· You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
· You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
· You can declare new methods in the subclass that are not in the superclass.
· You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.







Array

C++ Arrays are the data structures which can be used to store consecutive values of the same data types. C++ Arrays can be declared for all c++ data types viz., int, float, double, char, struct, char etc., All the values are stored in consecutive memory locations. The values can be accessed by using the position of the stored value

Declaring C++ Arrays of int type:
The c++ arrays are declared with the data type name and the number of elements inside the square brackets.
int var_name[50]; // C++ Array of type int with maximum size = 50.
The above declaration means, the var_name is an array of integer type. The values inside this var_name array can be accessed by referring to the position of their elements like var_name[0], var_name[1] etc.,
All the C++ arrays are based on Zero index values. That means the position reference starts at 0 and counts till the n-1th position. So in the above array, 50 elements can be stored starting from 0 to 49. The maximum number of elements that can be stored or the maximum size of the c++ array is 50. The values can be stored and accessed as given in the following code snippet.
var_name[0] = 0;
var_name[1] = 1;
var_name[2] = 30;
Declaring C++ arrays of type float:
The declaration and use of c++ arrays in float type are the same as int. The declaration is written as follows.
float var_float[100]; //A c++ array of float type
The above declaration means, the c++ float array contains 100 elements starting from position 0 till 99. The elements can be accessed as var_float[0], var_float[1], var_float[2] .. var_float[99].
var_float[0] = 10.05;
var_float[1] = 11.04;
var_float[99] = 87.65;
Declaring C++ arrays of type char:
This is one of the most widely used and problematic type of c++ array. When an array of char is declared, this char array can be used as a string with the maximum size as specified in the array declaration. When this c++ char array is declared with two dimensions, this can be thought as an array of strings.
char var_char_array[50]; //Memory reserved for 50 characters.
char var_char_init[] = "c++ char array string example"; // initialization without specifying the size
Like the above the c++ char array can be declared with or without initializing values.
C++ Array Advantages:
· Easier to declare and use.
· Can be used with all data types.
C++ Array Disadvantages:
· Fixed size data. If the number of elements stored are less than the maximum size, then the rest of the memory space goes waste.
· If the number of elements to be stored are more than the maximum size, the array cannot accommodate those new values.

<ENDL;






function overloading

Function Overloading
In order to overload a function, a different signature should be used for each overloaded version. A function signature consists of the list of types of its arguments as well as their order. Here's a set of valid overloaded versions of a function named f:

void f(char c, int i);
void f(int i, char c);
void f(string & s);
void f();
void f(int i);
void f(char c);

Function overloading is one of the most powerful features of C++ programming language. It forms the basis of polymorphism (compile-time polymorphism

you have to declare functions in such a way that they differ either in terms of the number of parameters or in terms of the type of parameters they take.

you just need to declare two or more functions having the same name but either having different number of parameters or having parameters of different types.

Example 1: Overloading Functions that differ in terms of NUMBER OF PARAMETERS
//Example Program in C++
#include<iostream.h>

//FUNTION PROTOTYPES
int func(int i);
int func(int i, int j);

void main(void)
{
cout<<func(10);//func(int i)is called

cout<<func(10,10);//func(int i, int j) is called
}

Overloading Functions that differ in terms of TYPE OF PARAMETERS
//Example Program in C++
#include<iostream.h>

//FUNTION PROTOTYPES
int func(int i);
double func(double i);

void main(void)
{
cout<<func(10);//func(int i)is called

cout<<func(10.201);//func(double i) is called
}


Class and object
Classes and objects are separate but related concepts. Every object belongs to a class and every class contains one or more related objects.

Ø A Class is static. All of the attributes of a class are fixed before, during, and after the execution of a program. The attributes of a class don't change.
Ø The class to which an object belongs is also (usually) static. If a particular object belongs to a certain class at the time that it is created then it almost certainly will still belong to that class right up until the time that it is destroyed.
Ø An Object on the other hand has a limited lifespan. Objects are created and eventually destroyed. Also during that lifetime, the attributes of the object may undergo significant change.

Concept of Class and Object
Class” refers to a blueprint. It defines the variables and methods the objects support
“Object” is an instance of a class. Each object has a class which defines its data and behavior
Classes reflect concepts, objects reflect instances that embody those concepts.
A class captures the common properties of the objects instantiated from it
A class characterizes the common behavior of all the objects that are its instances
Class
Visible in source code
The code is not duplicated
Object
Own copy of data
Active in running program
Occupies memory
Has the set of operations given in the class



Procedural vs. Object-Oriented Programming

The unit in procedural programming is function, and unit in object-oriented programming is class
Procedural programming concentrates on creating functions, while object-oriented programming starts from isolating the classes, and then look for the methods inside them.
Procedural programming separates the data of the program from the operations that manipulate the data, while object-oriented programming focus on both of them
procedural languages
Focus is on procedures
All data is shared: no protection
More difficult to modify
Hard to manage complexity

object oriented programming Object-oriented programming is a method of implementation in which programs are organized as cooperative collections of objects, each of which represents an instance of some class, and whose classes are all members of one or more hierarchy of classes united via inheritance relationships


object oriented programming

design n represent object
determine relationship between object
determine attribute each object has
determine behavior each object will respond to



procedural language

top down approach
create function to do small task
communicate by parameters and return values
Reply With Quote
The Following User Says Thank You to Janeeta For This Useful Post:
sweety03 (Wednesday, December 19, 2012)
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
WEb Building GLossary Terms Janeeta Computer Science 3 Monday, November 04, 2019 12:09 AM
EDS- notes Predator General Science Notes 70 Sunday, February 28, 2016 12:05 PM
Principles of Political Science Xeric Political Science 8 Friday, December 02, 2011 12:19 AM
Philosophy of Science A Rehman Pal Philosophy 0 Sunday, March 18, 2007 03:42 PM


CSS Forum on Facebook Follow CSS Forum on Twitter

Disclaimer: All messages made available as part of this discussion group (including any bulletin boards and chat rooms) and any opinions, advice, statements or other information contained in any messages posted or transmitted by any third party are the responsibility of the author of that message and not of CSSForum.com.pk (unless CSSForum.com.pk is specifically identified as the author of the message). The fact that a particular message is posted on or transmitted using this web site does not mean that CSSForum has endorsed that message in any way or verified the accuracy, completeness or usefulness of any message. We encourage visitors to the forum to report any objectionable message in site feedback. This forum is not monitored 24/7.

Sponsors: ArgusVision   vBulletin, Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.