CE-2810 Dr. Mark L. Hornick 1 “Classes” in C. CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints,

Slides:



Advertisements
Similar presentations
Incomplete Structs struct B; struct A { struct B * partner; // other declarations… }; struct B { struct A * partner; // other declarations… };
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
1 Structures. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc) and other.
Pointer Variables The normal variables hold values. For example, int j; j = 2; Then a reference to j in an expression will be identified with the value.
Structure.
Programming Languages and Paradigms The C Programming Language.
Programming Paradigms Introduction. 6/15/2005 Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved. L1:
Data Structures Using C++1 Chapter 3 Pointers and Array-Based Lists.
Structures Spring 2013Programming and Data Structure1.
Structs CSE 2451 Rong Shi. Structures Structure – one or more values, called members, with possibly dissimilar types that are stored together – Group.
C/c++ 4 Yeting Ge.
CS-1030 Dr. Mark L. Hornick 1 Constructors Copy Constructors.
Structures A structure is a collection of one or more variables, possibly of different types, grouped together under a single name for convenient handling.
Structures. An array allows us to store a collection of variables However, the variables must be of the same type to be stored in an array E.g. if we.
Heterogeneous Structures 4 Collection of values of possibly differing types 4 Name the collection; name the components 4 Example: student record harveyC.
What is a class? a class definition is a blueprint to build objects its like you use the blueprint for a house to build many houses in the same way you.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 6 : September 6.
C language issues CSC 172 SPRING 2002 EXTRA LECTURE.
Data Type. A data type defines a set of values that a variable can store along with a set of operations that can be performed on that variable. Common.
Learners Support Publications Pointers, Virtual Functions and Polymorphism.
Working with Structures. Structure Declaration ä Creates a user-defined data type. ä Gives the compiler a “head’s up” as to what the structure contains.
S-1 University of Washington Computer Programming I Lecture 18: Structures © 2000 UW CSE.
Structs. Structures We already know that arrays are many variables of the same type grouped together under the same name. Structures are like arrays except.
'C' Programming With Structure Records Purpose of structures Coding a structure template Defining a new data type Functions which communicate using structures.
Engineering Computing I Chapter 6 Structures. Sgtructures  A structure is a collection of one or more variables, possibly of different types, grouped.
Program structure Four different storage-class specifications: –Automatic (auto): local to a function, normally declared variables are automatic. Does.
Functions, Pointers, Structures Keerthi Nelaturu.
Parameters. Overview A Reminder Why Parameters are Needed How Parameters Work Value Parameters Reference Parameters Out Parameters.
CS-1030 Dr. Mark L. Hornick 1 Pointers are fun!
C++ Lecture 4 Tuesday, 15 July Struct & Classes l Structure in C++ l Classes and data abstraction l Class scope l Constructors and destructors l.
CS 108 Computing Fundamentals April 7, GHP # 10 and GHP 11 GHP 10 will be posted later today ­ Due: Saturday, April 11, at midnight GHP 11 will.
Object Oriented Programming (OOP) Lecture No. 11.
ECE 103 Engineering Programming Chapter 49 Structures Unions, Part 1 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
 Structures are like arrays except that they allow many variables of different types grouped together under the same name. For example you can create.
1 C Language Structures. 2 Topics Concept of a structure Concept of a structure Structures in c Structures in c Structure declaration Structure declaration.
Welcome to Concepts Pointer Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्जेण्डर )PGT(CS) KV JHAGRAKHAND.
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
1 Structs. 2 Defining a Structure Often need to keep track of several pieces of information about a given thing. Example: Box We know its length width.
ECE 103 Engineering Programming Chapter 50 Structures Unions, Part 2 Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE.
ENEE150 – 0102 ANDREW GOFFIN Project 4 & Function Pointers.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Classes in C++ And comparison to Java CS-1030 Dr. Mark L. Hornick.
CS-1010 Dr. Mark L. Hornick 1 Selection and Iteration and conditional expressions.
Structures 142 S -1 What is a structure? It is a collection of possibly different types Name the collection Name the components For example: a student.
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Dr. Mark L. HornickCS-1030 Dr. Mark Hornick 1 C++ Global functions Declarations & Definitions Preprocessor Directives.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
1 11/30/05CS150 Introduction to Computer Science 1 Structs.
Structures CSE 2031 Fall March Basics of Structures (6.1) struct point { int x; int y; }; keyword struct introduces a structure declaration.
1 Classes struct Public and Private Parts of a struct Class Scope of a Class Overloading Member Functions Class in a Class Static Members of Classes this.
ENEE150 – 0102 ANDREW GOFFIN Abstract Data Types.
Structs in C Computer Organization I 1 November 2009 © McQuain, Feng & Ribbens struct Properties The C struct mechanism is vaguely similar.
1 Structures & Unions. 2 User-Defined Types C provides facilities to define one’s own types. These may be a composite of basic types ( int, double, etc)
“Success consists of going from failure to failure without loss of enthusiasm.” Winston Churchill.
Java Programming Language Lecture27- An Introduction.
Structs versus Classes
CS1010 Programming Methodology
Homework / Exam Continuing K&R Chapter 6 Exam 2 after next class
Lecture 9 Structure 1. Concepts of structure Pointers of structures
Lecture 11 C Parameters Richard Gesick.
CS150 Introduction to Computer Science 1
Defining Your Own Classes Part 1
Basic C++ What’s a declaration? What’s a definition?
Pointer to Structures Lesson xx
Local Variables, Global Variables and Variable Scope
CS111 Computer Programming
CSCE 206 Lab Structured Programming in C
Classes Lecture 4 Secs 2.3, 4.1 – 4.3 Fri, Jan 26, 2007.
Class Circle { Float xc, yc, radius;
Presentation transcript:

CE-2810 Dr. Mark L. Hornick 1 “Classes” in C

CS-280 Dr. Mark L. Hornick 2 A struct is a complex datatype that can consist of Primitive datatypes Ints, floats, chars Arrays of any of the above Pointers to primitive datatypes Other structs or pointers to other structs

Is a struct something like a class? What’s missing? CS-280 Dr. Mark L. Hornick 3

CE-2810 Dr. Mark L. Hornick 4 Pointer review To define a pointer to a variable, we write: int* px; // unitialized pointer px = &x; // assume x is an int *px = 3; // set x=3 Point* pp1; // unitialized pp1 = &p1; // assume p1 is a Point pp1->x = 1; // set x member to 1

CE-2810 Dr. Mark L. Hornick 5 Function pointers Assume the functions with the following prototypes have been defined: int doSomething(int x, float y); int doSomethingElse(int x, float y); To define a compatible pointer pf to these functions, we write: int (*pf)(int, float);

CE-2810 Dr. Mark L. Hornick 6 Using a function pointer int doSomething(int x, float y); int doSomethingElse(int x, float y); int (*pf)(int, float); To assign the pointer to one of the functions: pf = doSomething; // NO PARENTHESES!! To call the function doSomething via the pointer: int status = pf(3, 4.0);

CE-2810 Dr. Mark L. Hornick 7 If a function is declared static, it can only be accessed from within the same source module (.c file) Essentially, the function is private to the module static int doSomething(int x, float y); Both prototype (declaration) and implementation (definition) have to be declared static.

Function pointers as struct members typedef struct Point { // declaration of Point struct float x; // x coordinate float y; // y coordinate int (*pf1)(int, float); // pointer to function void (*pf2)(void); // pointer to function } Point;// definition of Point datatype... Point pt1;// “object” of type Point; unitialized CS-280 Dr. Mark L. Hornick 8 Remember: local variables are not initialized in C global variables are initialized to 0

Initializing the struct Point pt1;// “object” of type Point; unitialized pt1.x = 3; Pt1.y = 4; Pt1.pf1 = ??? // initialize function pointers Pt1.pf2 = ??? Pt1.pf1(3,4.0); CS-280 Dr. Mark L. Hornick 9 Is there another way to initialize?